[Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/attrview ReferenceAttribute.java, NONE,
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2007-12-18 15:06:52
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv6216/src/net/sourceforge/bprocessor/gui/attrview Modified Files: LinkAttribute.java GenericPanel.java Added Files: ReferenceAttribute.java Log Message: Started refactoring of Attribute etc. Index: GenericPanel.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview/GenericPanel.java,v retrieving revision 1.50 retrieving revision 1.51 diff -C2 -d -r1.50 -r1.51 *** GenericPanel.java 17 Dec 2007 13:55:09 -0000 1.50 --- GenericPanel.java 18 Dec 2007 15:06:45 -0000 1.51 *************** *** 33,36 **** --- 33,37 ---- import net.sourceforge.bprocessor.model.Parametric; import net.sourceforge.bprocessor.model.Project; + import net.sourceforge.bprocessor.model.Reference; import net.sourceforge.bprocessor.model.Selector; import net.sourceforge.bprocessor.model.Structure; *************** *** 192,195 **** --- 193,204 ---- } + private void handleReference(Attribute attribute, JComponent where) { + ReferenceAttribute la = new ReferenceAttribute(attribute); + la.addStringAttributeListener(new GenericListener()); + genAttributes.add(la); + where.add(new AttributeRow(la)); + } + + private void handleOperation(Attribute attribute, JComponent where) { GenericAttribute generic = new OperationAttribute(attribute); *************** *** 228,231 **** --- 237,242 ---- } else if (a.getValue() instanceof Operation) { handleOperation(a, where); + } else if (a.getValue() instanceof Reference) { + handleReference(a, where); } else { System.out.println("-- " + a.getValue() + " --"); Index: LinkAttribute.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview/LinkAttribute.java,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** LinkAttribute.java 13 Dec 2007 12:00:55 -0000 1.18 --- LinkAttribute.java 18 Dec 2007 15:06:45 -0000 1.19 *************** *** 91,95 **** 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); --- 91,95 ---- component.add(createValueLabel(attribute.getValue())); ClassLoader classloader = Thread.currentThread().getContextClassLoader(); ! URL url = classloader.getResource("Biconarrow.gif"); icon = new JLabel(new ImageIcon(url)); this.add(component); --- NEW FILE: ReferenceAttribute.java --- //--------------------------------------------------------------------------------- // $Id: ReferenceAttribute.java,v 1.1 2007/12/18 15:06:45 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.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.Collections; import java.util.Comparator; 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 javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.PlainDocument; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Entity; import net.sourceforge.bprocessor.model.Geometric; import net.sourceforge.bprocessor.model.Project; import net.sourceforge.bprocessor.model.Reference; import net.sourceforge.bprocessor.model.Selection; import net.sourceforge.bprocessor.model.Container; /** * The LinkAttributeView */ public class ReferenceAttribute 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 AutoCompleteField editor; /** * Constructor for StringAttribute * @param attribute The attribute */ public ReferenceAttribute(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((Reference) attribute.getValue())); ClassLoader classloader = Thread.currentThread().getContextClassLoader(); URL url = classloader.getResource("Biconarrow.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(Reference reference) { Object value = reference.getObject(); String s = ""; JLabel valueLabel; if (value instanceof Entity) { if (value instanceof Container) { s = ((Container)value).getDisplayName(); } else { s = ((Entity)value).getName(); } } else if (value instanceof Project) { s = ((Project)value).getName(); } 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); } AutoCompleteField valueEditor = new AutoCompleteField(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 = (AutoCompleteField) 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()); } component.remove(editor); editor.removeKeyListener(this); editor = null; component.add(createValueLabel((Reference) 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((Reference) 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) { Object object = ((Reference) attribute().getValue()).getObject(); if (object instanceof Geometric) { Selection.primary().set((Geometric)object); } else { AttributeView.instance().display(object); } } /** * 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) { } /** * Autocomplete textfield */ public class AutoCompleteField extends JTextField { /** * constructor of autocomplete field * @param string is the number of colums in the field */ public AutoCompleteField(String string) { super(string); } /** * creates the document * @return a new autocompletedocument */ protected Document createDefaultModel() { return new AutoCompleteDocument(this); } } /** * The autocomplete document */ public class AutoCompleteDocument extends PlainDocument { /** The typed text */ private String typed; /** The textfield */ private JTextField field; /** * The AutoCompleteDocoment constructor * @param field the Textfield */ public AutoCompleteDocument(JTextField field) { this.field = field; typed = ""; } /** * insertString, insert a string into the document * @param offs the offset * @param str the string to be inserted * @param a the AttributeSet * @throws BadLocationException is thrown if the location is out of place */ public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null) { return; } if (offs < typed.length()) { typed = typed.substring(0, offs) + str + typed.substring(offs, typed.length()); } else { typed = typed + str; } super.remove(0, super.getLength()); typed = autocomplete(typed); super.insertString(0, typed, a); field.setCaretPosition(offs + str.length()); field.setSelectionStart(offs + str.length()); field.setSelectionEnd(typed.length()); } /** * Autocomplete the given string * @param string The string to autocomplete * @return The result of the completion that is the given string if no completion */ public String autocomplete(String string) { if (!string.equalsIgnoreCase("")) { final Comparator alpha = new Comparator() { public int compare(Object s1, Object s2) { return ((Container) s1).getName().compareToIgnoreCase(((Container) s2).getName()); } }; final Comparator num = new Comparator() { public int compare(Object s1, Object s2) { return ((Container) s1).getId().compareTo(((Container) s2).getId()); } }; LinkedList sort = new LinkedList(Project.getInstance().getSpaces()); Collections.sort(sort, alpha); Collections.sort(sort, num); LinkedList matches = new LinkedList(); Iterator it = sort.iterator(); while (it.hasNext()) { Container current = (Container) it.next(); String name = current.getName() + "-" + current.getId().toString(); if (name.startsWith(string) && !name.equalsIgnoreCase(string)) { matches.add(name); return name; } } } return string; } /** * @param offs the ofset to remove from * @param amount The number of characters to remove * @throws BadLocationException if characters that do not exist is beeing removed */ public void remove(int offs, int amount) throws BadLocationException { //FIXME String typed1 = typed.substring(0, offs); String typed2 = typed.substring(offs + amount, typed.length()); typed = typed1.concat(typed2); //String test = autocomplete(typed); super.remove(offs, amount); } } } |