Thread: [Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/attributesview AttributesPanel.java,NONE,
Status: Pre-Alpha
Brought to you by:
henryml
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attributesview In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19328/src/net/sourceforge/bprocessor/gui/attributesview Added Files: AttributesPanel.java AttributeView.java package.html StringAttributeListener.java StringAttributeView.java Log Message: New attributes view code --- NEW FILE: StringAttributeView.java --- //--------------------------------------------------------------------------------- // $Id: StringAttributeView.java,v 1.1 2006/01/05 13:28:02 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.attributesview; import java.awt.Component; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.swing.Box; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JTextField; /** * The StringAttributeView */ public class StringAttributeView implements AttributeView, KeyListener { /** The listeners */ private List listeners; /** The name of this AttributeView */ private String name; /** The String value of this AttributeView */ private String value; /** The component */ private JComponent component; /** The editor, when this AttributeView is being edited */ private JTextField editor; /** * Constructor for StringAttributeView * @param name The name * @param value The value */ public StringAttributeView(String name, String value) { super(); this.name = name; this.value = value; this.listeners = new LinkedList(); component = Box.createHorizontalBox(); component.add(createValueLabel(value)); } /** * Add a listener that are notified when the value is changed * @param listener The listener */ public void addStringAttributeListener(StringAttributeListener listener) { listeners.add(listener); } /** * Remove a listener * @param listener The listener */ public void removeStringAttributeListener(StringAttributeListener listener) { listeners.remove(listener); } /** * Send valueChanged to all listeners */ protected void valueChanged() { Iterator iter = listeners.iterator(); while (iter.hasNext()) { StringAttributeListener current = (StringAttributeListener) iter.next(); current.valueChanged(name, value); } } /** * Create a value label * @param value The value * @return The label */ protected JComponent createValueLabel(String value) { JLabel valueLabel = new JLabel(value); valueLabel.setFont(AttributesPanel.VALUE_FONT); return valueLabel; } /** * Create a value editor * @param value The value * @return The editor */ JComponent createValueEditor(String value) { JTextField valueEditor = new JTextField(value); valueEditor.setFont(AttributesPanel.VALUE_FONT); return valueEditor; } /** * Return the name * @return The name */ public String name() { return name; } /** * Return the component * @return The component */ public JComponent component() { return component; } /** * Start editing the value by replacing the label with an editor */ public void startEditing() { if (editor == null) { component.remove(0); editor = (JTextField) createValueEditor(value); 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) { component.remove(editor); value = editor.getText(); editor.removeKeyListener(this); editor = null; component.add(createValueLabel(value)); 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(value)); component.revalidate(); } } /** * Find the AttributesPanel this AttributeView belongs to and * ask it to stopEditing the current AttributeView, which will * be this one */ public void forceStopEditing() { Component current = editor; while (current != null) { if (current instanceof AttributesPanel) { AttributesPanel panel = (AttributesPanel) current; panel.stopEditing(); current = null; } else { current = current.getParent(); } } } /** * Respond to the enter key by stopping editing. * @param event The KeyEvent */ public void keyPressed(KeyEvent event) { if (event.getKeyCode() == KeyEvent.VK_ENTER) { forceStopEditing(); } } /** * Not in use * @param event The KeyEvent */ public void keyTyped(KeyEvent event) { } /** * Not in use * @param event The KeyEvent */ public void keyReleased(KeyEvent event) { } } --- NEW FILE: package.html --- <body> This package defines functionality to show attributes for an object </body> --- NEW FILE: AttributesPanel.java --- //--------------------------------------------------------------------------------- // $Id: AttributesPanel.java,v 1.1 2006/01/05 13:28:02 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.attributesview; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Iterator; import java.util.List; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JLabel; import javax.swing.JPanel; /** * The AttributesPanel */ public class AttributesPanel extends JPanel implements MouseListener { /** The value font */ public static final Font VALUE_FONT = new Font("Sans-serif", Font.PLAIN, 12); /** The key font */ public static final Font KEY_FONT = new Font("Sans-serif", Font.BOLD, 12); /** The column - a vertical box */ private Box column; /** The list of AttributeView */ private List attributes; /** The currently edited AttributeView */ private AttributeView current; /** * Constructor for AttributesPanel */ public AttributesPanel() { super(); setLayout(new BorderLayout()); column = Box.createVerticalBox(); column.addMouseListener(this); add(column); } /** * Set the attributes in this AttributesPanel * @param attributes List of AttributeView */ public void setAttributes(List attributes) { column.removeAll(); this.attributes = attributes; if (attributes != null) { Iterator iter = attributes.iterator(); while (iter.hasNext()) { AttributeView current = (AttributeView) iter.next(); AttributeRow row = new AttributeRow(current); column.add(row); } } column.revalidate(); } /** * Get the attributes in this AttributesPanel * @return List of AttributeView */ public List getAttributes() { return attributes; } /** * Start editing the specified AttributeView * @param view The AttributeView to edit */ public void startEditing(AttributeView view) { if (current != null) { current.stopEditing(); } current = view; if (current != null) { view.startEditing(); } } /** * Stop editing the current AttributeView */ public void stopEditing() { if (current != null) { current.stopEditing(); current = null; } } /** * The AttributeRow */ protected class AttributeRow extends Box implements MouseListener { /** The AttributeView displayed in this AttributeRow */ private AttributeView view; /** * Constructor for AttributeRow * @param view The AttributeView for this AttributeRow */ public AttributeRow(AttributeView view) { super(BoxLayout.X_AXIS); this.view = view; JLabel label = createLabel(view.name()); Box header = Box.createHorizontalBox(); Box column = Box.createVerticalBox(); header.add(Box.createHorizontalGlue()); header.add(label); column.add(Box.createHorizontalStrut(60)); column.add(header); column.add(Box.createHorizontalStrut(60)); column.setMaximumSize(new Dimension(60, Short.MAX_VALUE)); add(column); add(Box.createHorizontalStrut(7)); add(view.component()); add(Box.createHorizontalGlue()); label.addMouseListener(this); view.component().addMouseListener(this); } /** * Create a label for the * @param key The key * @return A JLabel */ private JLabel createLabel(String key) { JLabel keyLabel = new JLabel(key + " :"); keyLabel.setFont(KEY_FONT); return keyLabel; } /** * Start editing on the view * @param event The event */ public void mouseClicked(MouseEvent event) { startEditing(view); } /** * 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) { } } /** * Stop editing the current attribute * @param event The mouse event */ public void mouseClicked(MouseEvent event) { stopEditing(); } /** * 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) { } } --- NEW FILE: AttributeView.java --- //--------------------------------------------------------------------------------- // $Id: AttributeView.java,v 1.1 2006/01/05 13:28:02 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.attributesview; import javax.swing.JComponent; /** * The AttributeView */ public interface AttributeView { /** * Return the name for this AttributeView * @return The name */ public String name(); /** * Get the component for this AttributeView * @return The component */ public JComponent component(); /** * Called when editing is started on this AttributeView * Should change the component to an editing state */ public void startEditing(); /** * Called when editing is stopped on this AttributeView * Should send a valueChanged to any listeners in concrete AttributeViews, * and change the component to the default non-editing state */ public void stopEditing(); /** * Called when editing should be cancelled on this AttributeView. * Should change the state back to before editing was started. * Should not send events the listeners. */ public void cancelEditing(); } --- NEW FILE: StringAttributeListener.java --- //--------------------------------------------------------------------------------- // $Id: StringAttributeListener.java,v 1.1 2006/01/05 13:28:02 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.attributesview; import java.util.EventListener; /** * The StringAttributeListener */ public interface StringAttributeListener extends EventListener { /** * Called when the value has changed * @param name The name of the attribute * @param value The new value for the attribute */ public void valueChanged(String name, String value); } |