/*
*
* Perforce/JBuilder Opentool
* Copyright (C) 2002 Mark Ackerman
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package com.dafreels.opentools.actions.ui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
import com.dafreels.opentools.*;
import com.dafreels.vcs.command.*;
/**
* <p>Title: Perforce Open Tool </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: RMA</p>
* @author Mark Ackerman
*
*/
public class DescribeDialog extends JDialog
{
private static final String LEAD_IN = "... ";
private static final Color GRAY = new Color(200,200,200);
private JTextPane _descriptionText;
private String _changeListNumber;
private Vector _changeList;
private JButton _diffsButton;
public DescribeDialog(Frame frame, String title, boolean modal,
String changeListNumber, Vector changeList )
{
super(frame, title, modal);
_changeListNumber = changeListNumber;
_changeList = changeList;
buildControls();
}
protected void buildControls()
{
getContentPane().setLayout(new GridBagLayout());
_descriptionText = new JTextPane()
{
public Dimension getPreferredScrollableViewportSize()
{
Dimension d = super.getPreferredScrollableViewportSize();
d.width = 500;
d.height = 200;
return d;
}
};
_descriptionText.setEditable(false);
//_descriptionText.setWrapStyleWord(true);
//_descriptionText.setLineWrap(true);
_descriptionText.setBackground(null);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,0,5);
getContentPane().add(new JScrollPane(_descriptionText), gbc);
JPanel panel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.;
gbc.weighty = 0.;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5,5,0,5);
getContentPane().add(panel, gbc);
JButton upbutton = new JButton(getImageIcon("com/borland/primetime/actions/image/up.gif"));
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.;
gbc.weighty = 0.;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.insets = new Insets(5,5,0,5);
panel.add(upbutton, gbc);
JButton downbutton = new JButton(getImageIcon("com/borland/primetime/actions/image/down.gif"));
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.;
gbc.weighty = 0.;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.insets = new Insets(5,5,0,5);
panel.add(downbutton, gbc);
_diffsButton = new JButton("Show Diffs");
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.;
gbc.weighty = 0.;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5,5,0,5);
panel.add(_diffsButton, gbc);
JButton okbutton = new JButton("Close");
gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.;
gbc.weighty = 0.;
gbc.anchor = GridBagConstraints.SOUTHEAST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5,5,0,5);
panel.add(okbutton, gbc);
okbutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
});
_diffsButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
runDescribe(true);
_diffsButton.setVisible(false);
}
});
if ( _changeList == null || _changeList.size() == 0 )
{
upbutton.setVisible(false);
downbutton.setVisible(false);
}
else
{
upbutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
previousChange();
}
});
downbutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
nextChange();
}
});
}
}
/**
* fill this dialog with the contents of formatter
* @param formatter the output from the <code>fstat</code> command
*/
public void fillForm(MessageFormatter formatter)
{
_descriptionText.setText("");
String message, arg;
SimpleAttributeSet attrs;
PropertyInterface props = Main.m_props;
DefaultListModel model = new DefaultListModel();
formatter.getNextMessage(); // throw away first line
MessageWriter.outputErrorMessages(MessageFormatter.getInstance());
while( (message = formatter.getNextMessage()) != null)
{
if ( message.startsWith(LEAD_IN))
{
message = message.substring(4);
}
message = message.trim();
message = message.concat("\n");
if ( message.startsWith("//"))
{
appendFileLine(message);
}
else
{
attrs = new SimpleAttributeSet();
StyleConstants.setForeground(attrs, UIManager.getColor("TextField.foreground"));
StyleConstants.setBackground(attrs, GRAY);
appendText(message, attrs);
}
//_descriptionText.append("\n");
}
_descriptionText.setCaretPosition(0);
if( !isVisible() )
{
pack();
setLocation();
}
}
protected void appendFileLine(String message)
{
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setForeground(attrs, Color.blue);
StyleConstants.setBackground(attrs, GRAY);
StyleConstants.setUnderline(attrs, true);
int idx = message.lastIndexOf(" ");
if ( idx == -1 )
{
appendText(message, attrs);
}
else
{
String file = message.substring(0, idx);
appendText(file, attrs);
attrs = new SimpleAttributeSet();
StyleConstants.setForeground(attrs, UIManager.getColor("TextField.foreground"));
StyleConstants.setBackground(attrs, GRAY);
appendText(message.substring(idx), attrs);
}
}
protected void appendText(String message, SimpleAttributeSet attrs)
{
Font f = (Font)UIManager.get("TextField.font");
StyleConstants.setFontFamily(attrs,f.getFamily());
StyleConstants.setFontSize(attrs, f.getSize()+1);
try
{
Document doc = _descriptionText.getDocument();
int len = doc.getLength();
doc.insertString(len, message, attrs);
}
catch (BadLocationException ex)
{
System.out.println(ex);
}
}
/**
* set the dialogs location centered on its parent. If it doesn't
* hava a parent then center it on the screen.
*/
public void setLocation()
{
Component parent = getParent();
int ix, iy, bx, by;
Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Dimension dd = getSize();
if ( parent == null )
{
ix = (d.width-dd.width)/2;
iy = (d.height-dd.height)/2;
}
else
{
Rectangle bounds = parent.getBounds();
Rectangle abounds = getBounds();
ix = bounds.x + (bounds.width - abounds.width)/ 2;
iy = bounds.y + (bounds.height - abounds.height)/2;
}
if(iy < 10) iy = 10;
if(ix < 10) ix = 10;
bx = ix+dd.width;
by = iy+dd.height;
// see if the dialog is off the screen.
if ( bx > d.width )
{
ix -=bx-d.width;
}
if ( by > d.height )
{
iy -= by-d.height;
}
setLocation(ix,iy);
}
public void runDescribe(boolean showDiffs)
{
if ( _changeListNumber == null )
{
return;
}
try
{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
StringBuffer sb = new StringBuffer("describe ");
if ( !showDiffs )
{
sb.append("-s ");
}
sb.append(_changeListNumber);
//Run the command
CommandTool.runCommand(sb.toString(), Main.m_props);
//Output the p4 messages
fillForm(MessageFormatter.getInstance());
}
finally
{
setCursor(Cursor.getDefaultCursor());
}
}
protected void previousChange()
{
if ( _changeList == null )
{
return;
}
int idx = _changeList.indexOf(_changeListNumber);
if ( idx >= _changeList.size() || idx < 1)
{
return;
}
idx--;
_changeListNumber = (String)_changeList.get(idx);
runDescribe(!_diffsButton.isVisible());
}
protected void nextChange()
{
if ( _changeList == null )
{
return;
}
int idx = _changeList.indexOf(_changeListNumber);
if ( idx >= _changeList.size() || idx == -1)
{
return;
}
idx++;
_changeListNumber = (String)_changeList.get(idx);
runDescribe(!_diffsButton.isVisible());
}
/**
* Retrieves a ImageIcon for a particular path. The path could be a path for a system resource,
* or a file path. This method checks the system resources first. If nothing is found, it checks
* the file path. If
*/
public static javax.swing.ImageIcon getImageIcon(String name )
{
if ( name == null || name.length() == 0 ) return null;
try
{
java.net.URL url = ClassLoader.getSystemResource(name);
if ( url == null ) {
java.io.File f = new java.io.File(name);
url = f.toURL();
if(url == null) return null;
}
return getImageIcon(url);
}
catch (Exception e)
{
System.out.println("getImageIcon: Error loading " + name +" Error: " + e);
e.printStackTrace();
}
return null;
}
public static javax.swing.ImageIcon getImageIcon(java.net.URL url) {
java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
if ( tk == null ) return null;
java.awt.Image image = tk.getImage(url);
if ( image == null ) return null;
return new javax.swing.ImageIcon(image);
}
}