Thread: [Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/attrview StringAttribute.java,1.10,1.11 L
Status: Pre-Alpha
Brought to you by:
henryml
|
From: Nikolaj B. <nbr...@us...> - 2006-04-24 08:45:17
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12251/src/net/sourceforge/bprocessor/gui/attrview Modified Files: StringAttribute.java LinkAttribute.java GenericPanel.java Log Message: Implemented the new link functionality. Links can now be links, but at the same time you can change the link if wanted. Index: StringAttribute.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview/StringAttribute.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** StringAttribute.java 16 Mar 2006 10:16:55 -0000 1.10 --- StringAttribute.java 24 Apr 2006 08:45:10 -0000 1.11 *************** *** 24,28 **** import net.sourceforge.bprocessor.model.Attribute; - import net.sourceforge.bprocessor.model.Entity; /** --- 24,27 ---- *************** *** 151,171 **** String s = ""; JLabel valueLabel; ! if (value instanceof Entity) { ! s = ((Entity)value).getName(); ! valueLabel = new JLabel(s); ! valueLabel.setFont(AttributeView.FONT_ITALIC); } else { ! if (value instanceof String) { ! s = (String)value; ! } else if (value instanceof Double) { ! double rounded = round(((Double) value).doubleValue()); ! s = Double.toString(rounded); ! } ! valueLabel = new JLabel(s); ! if (attribute.isEditable()) { ! valueLabel.setFont(AttributeView.FONT_PLAIN); ! } else { ! valueLabel.setFont(AttributeView.FONT_ITALIC); ! } } return valueLabel; --- 150,164 ---- String s = ""; JLabel valueLabel; ! if (value instanceof String) { ! s = (String)value; ! } else if (value instanceof Double) { ! double rounded = round(((Double) value).doubleValue()); ! s = Double.toString(rounded); ! } ! valueLabel = new JLabel(s); ! if (attribute.isEditable()) { ! valueLabel.setFont(AttributeView.FONT_PLAIN); } else { ! valueLabel.setFont(AttributeView.FONT_ITALIC); } return valueLabel; Index: GenericPanel.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview/GenericPanel.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** GenericPanel.java 10 Apr 2006 11:54:39 -0000 1.16 --- GenericPanel.java 24 Apr 2006 08:45:10 -0000 1.17 *************** *** 32,36 **** import net.sourceforge.bprocessor.model.Parametric; import net.sourceforge.bprocessor.model.Project; - import net.sourceforge.bprocessor.model.Selection; /** --- 32,35 ---- *************** *** 190,199 **** stopEditing(); current = ga; ! if (ga instanceof LinkAttribute) { ! Selection.primary().set(ga.attribute().getValue()); ! } else { ! if (current != null) { ! current.startEditing(); ! } } } --- 189,194 ---- stopEditing(); current = ga; ! if (current != null) { ! current.startEditing(); } } Index: LinkAttribute.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview/LinkAttribute.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** LinkAttribute.java 16 Mar 2006 10:16:55 -0000 1.5 --- LinkAttribute.java 24 Apr 2006 08:45:10 -0000 1.6 *************** *** 6,27 **** //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.attrview; ! import net.sourceforge.bprocessor.model.Attribute; /** * The LinkAttributeView */ ! public class LinkAttribute extends StringAttribute { /** ! * Constructor * @param attribute The attribute */ public LinkAttribute(Attribute attribute) { ! super(attribute); } } --- 6,316 ---- //--------------------------------------------------------------------------------- + package net.sourceforge.bprocessor.gui.attrview; ! import java.awt.Dimension; ! import java.awt.event.KeyEvent; ! import java.awt.event.KeyListener; ! import java.awt.event.MouseEvent; ! import java.awt.event.MouseListener; ! import java.net.URL; ! import java.util.Iterator; ! import java.util.LinkedList; ! import java.util.List; + import javax.swing.Box; + import javax.swing.BoxLayout; + import javax.swing.ImageIcon; + import javax.swing.JComponent; + import javax.swing.JLabel; + import javax.swing.JTextField; + + import net.sourceforge.bprocessor.model.Attribute; + import net.sourceforge.bprocessor.model.Entity; + import net.sourceforge.bprocessor.model.Selection; /** * The LinkAttributeView */ ! public class LinkAttribute extends GenericAttribute implements KeyListener, MouseListener { + /** The listeners */ + private List listeners; + + /** The attribute this gui represents */ + private Attribute attribute; + + /** The component */ + private JComponent component; + + /** The Label */ + private JLabel label; + + /** The link icon */ + private JLabel icon; + + /** The editor, when this EditableAttribute is being edited */ + private JTextField editor; + + + + /** ! * Constructor for StringAttribute * @param attribute The attribute */ public LinkAttribute(Attribute attribute) { ! super(BoxLayout.X_AXIS); ! ! this.attribute = attribute; ! this.listeners = new LinkedList(); ! ! label = createLabel(attribute.getName()); ! ! Box header = Box.createHorizontalBox(); ! Box column = Box.createVerticalBox(); ! header.add(Box.createHorizontalStrut(7)); ! header.add(label); ! header.add(Box.createHorizontalGlue()); ! column.add(Box.createRigidArea(new Dimension(70, 3))); ! column.add(header); ! column.add(Box.createRigidArea(new Dimension(70, 3))); ! column.setMaximumSize(new Dimension(60, Short.MAX_VALUE)); ! this.add(column); ! component = Box.createHorizontalBox(); ! component.add(createValueLabel(attribute.getValue())); ! ClassLoader classloader = Thread.currentThread().getContextClassLoader(); ! URL url = classloader.getResource("Biconlink.gif"); ! icon = new JLabel(new ImageIcon(url)); ! this.add(component); ! this.add(icon); ! this.add(Box.createHorizontalGlue()); ! } + /** + * Round + * @param value The value + * @return The rounded value + */ + protected double round(double value) { + int i = (int) (value * 100000); + return ((double) i) / 100000.0; + } + + /** + * Create a label for the + * @param key The key + * @return A JLabel + */ + private JLabel createLabel(String key) { + JLabel keyLabel = new JLabel(key + " :"); + keyLabel.setFont(AttributeView.FONT_BOLD); + return keyLabel; + } + + /** + * Add a mouselisterner to the active elements in the LinkAttribute + * @param mouseListener the mouselistener + */ + public void addMouseListener(MouseListener mouseListener) { + label.addMouseListener(mouseListener); + component.addMouseListener(mouseListener); + icon.addMouseListener(this); + } + + /** + * Remove a mouselisterner to the active elements in the LinkAttribute + * @param mouseListener the mouselistener + */ + public void removeMouseListener(MouseListener mouseListener) { + label.removeMouseListener(mouseListener); + component.removeMouseListener(mouseListener); + } + + /** + * Add a listener that are notified when the value is changed + * @param listener The listener + */ + public void addStringAttributeListener(AttributeListener listener) { + listeners.add(listener); + } + + /** + * Remove a listener + * @param listener The listener + */ + public void removeStringAttributeListener(AttributeListener listener) { + listeners.remove(listener); + } + + /** + * Send valueChanged to all listeners + */ + protected void valueChanged() { + Iterator iter = listeners.iterator(); + while (iter.hasNext()) { + AttributeListener current = (AttributeListener) iter.next(); + current.valueChanged(attribute); + } + } + + /** + * Create a value label + * @param value The value + * @return The label + */ + private JComponent createValueLabel(Object value) { + String s = ""; + JLabel valueLabel; + if (value instanceof Entity) { + s = ((Entity)value).getName(); + valueLabel = new JLabel(s); + } else { + if (value instanceof String) { + s = (String)value; + } else if (value instanceof Double) { + double rounded = round(((Double) value).doubleValue()); + s = Double.toString(rounded); + } + valueLabel = new JLabel(s); + } + if (attribute.isEditable()) { + valueLabel.setFont(AttributeView.FONT_PLAIN); + } else { + valueLabel.setFont(AttributeView.FONT_ITALIC); + } + return valueLabel; + } + + /** + * Create a value editor + * @param value The value + * @return The editor + */ + private JComponent createValueEditor(Object value) { + String s = ""; + if (value instanceof String) { + s = (String)value; + } else if (value instanceof Double) { + double rounded = round(((Double) value).doubleValue()); + s = Double.toString(rounded); + } + JTextField valueEditor = new JTextField(s); + valueEditor.setFont(AttributeView.FONT_PLAIN); + + return valueEditor; + } + + + /** + * Return the attribute + * @return The attribute + */ + public Attribute attribute() { + return attribute; + } + + /** + * Start editing the value by replacing the label with an editor + */ + public void startEditing() { + if (attribute.isEditable()) { + if (editor == null) { + component.remove(0); + editor = (JTextField) createValueEditor(attribute.getValue()); + editor.addKeyListener(this); + component.add(editor); + component.revalidate(); + } + editor.requestFocus(); + editor.selectAll(); + } + } + + /** + * Stop editing the value by replacing the editor with an label, + * and sending valueChanged to all listeners. + */ + public void stopEditing() { + if (editor != null && attribute.isEditable()) { + Object val = attribute.getValue(); + if (val instanceof String || val instanceof Entity) { + attribute.setValue(editor.getText()); + } else if (val instanceof Double) { + attribute.setValue(Double.valueOf(editor.getText())); + } + component.remove(editor); + editor.removeKeyListener(this); + editor = null; + component.add(createValueLabel(attribute.getValue())); + component.revalidate(); + valueChanged(); + } + } + + /** + * Cancels editing the value by replacing the editor with an label, + * without sending any events to listeners. + */ + public void cancelEditing() { + if (editor != null) { + component.remove(editor); + editor.removeKeyListener(this); + editor = null; + component.add(createValueLabel(attribute.getValue())); + component.revalidate(); + } + } + + /** + * Respond to the enter key by stopping editing. + * @param event The KeyEvent + */ + public void keyPressed(KeyEvent event) { + if (event.getKeyCode() == KeyEvent.VK_ENTER) { + stopEditing(); + } else if (event.getKeyCode() == KeyEvent.VK_ESCAPE) { + cancelEditing(); + } + } + + /** + * Not in use + * @param event The KeyEvent + */ + public void keyTyped(KeyEvent event) { } + /** + * Not in use + * @param event The KeyEvent + */ + public void keyReleased(KeyEvent event) { } + + /** + * Start editing on the view + * @param event The event + */ + public void mouseClicked(MouseEvent event) { + Selection.primary().set(attribute().getValue()); } + /** + * Not in use + * @param event The MouseEvent + */ + public void mousePressed(MouseEvent event) { } + /** + * Not in use + * @param event The MouseEvent + */ + public void mouseReleased(MouseEvent event) { } + /** + * Not in use + * @param event The MouseEvent + */ + public void mouseEntered(MouseEvent event) { } + /** + * Not in use + * @param event The MouseEvent + */ + public void mouseExited(MouseEvent event) { } } |