[Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/attrview ClassificationIdAttribute.java,
Status: Pre-Alpha
Brought to you by:
henryml
From: Nikolaj B. <nbr...@us...> - 2006-11-10 13:47:51
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv29942/src/net/sourceforge/bprocessor/gui/attrview Modified Files: ClassificationIdAttribute.java ClassificationTextAttribute.java LinkAttribute.java Log Message: Further implementation of the classificaion view Index: ClassificationTextAttribute.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview/ClassificationTextAttribute.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ClassificationTextAttribute.java 30 Oct 2006 00:00:02 -0000 1.1 --- ClassificationTextAttribute.java 10 Nov 2006 13:47:41 -0000 1.2 *************** *** 11,27 **** import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JComponent; import javax.swing.JMenu; - import javax.swing.JMenuBar; import javax.swing.JMenuItem; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Classification; /** --- 11,41 ---- import java.awt.event.ActionEvent; import java.awt.event.ActionListener; + 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 java.util.StringTokenizer; + import java.util.Vector; import javax.swing.Box; import javax.swing.BoxLayout; + import javax.swing.ImageIcon; import javax.swing.JComponent; + import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuItem; + import javax.swing.JPopupMenu; + import javax.swing.JTextField; + import javax.swing.text.AttributeSet; + import javax.swing.text.BadLocationException; + import javax.swing.text.PlainDocument; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Classification; + import net.sourceforge.bprocessor.model.Project; /** *************** *** 29,33 **** */ public class ClassificationTextAttribute extends GenericAttribute ! implements ActionListener { /** The listeners */ --- 43,47 ---- */ public class ClassificationTextAttribute extends GenericAttribute ! implements ActionListener, MouseListener, KeyListener { /** The listeners */ *************** *** 39,43 **** /** The component */ private JComponent component; ! --- 53,66 ---- /** The component */ private JComponent component; ! ! /** The link icon */ ! private JLabel icon; ! ! /** The popup menu */ ! private JPopupMenu classification; ! ! ! /** The editor, when this EditableAttribute is being edited */ ! private AutoCompleteField editor; *************** *** 53,57 **** Box column = Box.createVerticalBox(); component = Box.createHorizontalBox(); ! component.add(createMenu(attribute.getValue())); Box header = Box.createHorizontalBox(); header.add(Box.createHorizontalStrut(7)); --- 76,85 ---- Box column = Box.createVerticalBox(); component = Box.createHorizontalBox(); ! if (attribute.getClassification() != null) { ! component.add((JLabel) createValueLabel(attribute.getClassification())); ! } else { ! component.add(new JLabel("Classification")); ! } ! createMenu(attribute.getValue()); Box header = Box.createHorizontalBox(); header.add(Box.createHorizontalStrut(7)); *************** *** 61,69 **** --- 89,120 ---- column.add(header); column.add(Box.createRigidArea(new Dimension(70, 3))); + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); + URL url = classloader.getResource("Biconlink.gif"); + icon = new JLabel(new ImageIcon(url)); this.add(column); + this.add(icon); } /** + * Add a mouselisterner to the active elements in the ClassificationTextAttribute + * @param mouseListener the mouselistener + */ + public void addMouseListener(MouseListener mouseListener) { + component.addMouseListener(mouseListener); + icon.addMouseListener(this); + } + + /** + * Remove a mouselisterner to the active elements in the ClassificationTextAttribute + * @param mouseListener the mouselistener + */ + public void removeMouseListener(MouseListener mouseListener) { + component.removeMouseListener(mouseListener); + icon.removeMouseListener(this); + } + + + /** * Add a listener that are notified when the value is changed * @param listener The listener *************** *** 95,110 **** * Create a value label * @param value The value - * @return The label */ ! private JComponent createMenu(Object value) { ! JMenuBar valueLabel = new JMenuBar(); ! JMenu menu, subMenu; MyMenuItem mi, submi; boolean hasChildren = false; - if (attribute.getClassification() != null) { - menu = new JMenu(attribute.getClassification().getName()); - } else { - menu = new JMenu("Classification"); - } Iterator it = ((Classification) value).getChildren().iterator(); while (it.hasNext()) { --- 146,155 ---- * Create a value label * @param value The value */ ! private void createMenu(Object value) { ! classification = new JPopupMenu(); ! JMenu subMenu; MyMenuItem mi, submi; boolean hasChildren = false; Iterator it = ((Classification) value).getChildren().iterator(); while (it.hasNext()) { *************** *** 125,141 **** subMenu.add(submi); } ! menu.add(subMenu); } else { mi = new MyMenuItem(current); mi.addActionListener(this); ! menu.add(mi); } } - valueLabel.add(menu); - return valueLabel; } /** --- 170,220 ---- subMenu.add(submi); } ! classification.add(subMenu); } else { mi = new MyMenuItem(current); mi.addActionListener(this); ! classification.add(mi); } } } + /** + * Create a value label + * @param value The value + * @return The label + */ + private JComponent createValueLabel(Classification value) { + JLabel valueLabel; + if (attribute.getClassification().getId().intValue() == -1) { + valueLabel = new JLabel(value.getName()); + } else { + if (attribute.getClassification() == null || + ((Classification)attribute.getValue()).getName(). + equalsIgnoreCase(attribute.getClassification().getParent().getName())) { + valueLabel = new JLabel(value.getName()); + } else { + //valueLabel = new JLabel(value.getParent().getName() + "." + value.getName()); + valueLabel = new JLabel(value.getName()); + } + } + return valueLabel; + } + /** + * Create a value editor + * @param value The value + * @param name the current name + * @return The value editor + */ + private JComponent createValueEditor(String name, Classification value) { + String s = ""; + if (!name.equalsIgnoreCase("Classification")) { + s = name; + } + AutoCompleteField valueEditor = new AutoCompleteField(s, (Classification) attribute.getValue()); + valueEditor.setFont(AttributeView.FONT_PLAIN); + return valueEditor; + } /** *************** *** 146,150 **** return attribute; } - /** --- 225,228 ---- *************** *** 193,218 **** /** ! * Not in use */ public void startEditing() { ! // TODO Auto-generated method stub ! } /** * Not in use */ ! public void stopEditing() { ! // TODO Auto-generated method stub ! } /** * Not in use */ ! public void cancelEditing() { ! // TODO Auto-generated method stub } } --- 271,523 ---- /** ! * Start editing */ public void startEditing() { ! if (editor == null) { ! editor = (AutoCompleteField) createValueEditor(((JLabel)component.getComponent(0)) ! .getText(), attribute.getClassification()); ! component.remove(0); ! editor.addKeyListener(this); ! component.add(editor); ! component.revalidate(); ! } ! editor.requestFocus(); ! editor.selectAll(); } + + /** + * Stop editing + */ + public void stopEditing() { + if (editor != null && attribute.isEditable()) { + Vector cur = ((Classification) attribute.getValue()).getChildren(); + Classification current; + Classification entered = null; + StringTokenizer st = new StringTokenizer(editor.getText(), "."); + while (st.hasMoreTokens()) { + String name = st.nextToken(); + Iterator it = cur.iterator(); + while (it.hasNext()) { + current = (Classification) it.next(); + if (current.getName().equalsIgnoreCase(name)) { + cur = current.getChildren(); + entered = current; + } + } + } + if (entered != null) { + attribute.setClassification(entered); + } else { + Classification newclas = new Classification(editor.getText()); + Project.getInstance().getClassification().addChild(newclas); + attribute.setClassification(newclas); + } + component.remove(editor); + editor.removeKeyListener(this); + editor = null; + component.add(createValueLabel((Classification) attribute.getClassification())); + component.revalidate(); + valueChanged(); + } + } + + /** + * Not in use + */ + public void cancelEditing() { + if (editor != null) { + component.remove(editor); + editor.removeKeyListener(this); + editor = null; + component.add(createValueLabel((Classification) attribute.getValue())); + component.revalidate(); + } + } + + /** + * Start editing on the view + * @param event The event + */ + public void mouseClicked(MouseEvent event) { } /** * Not in use + * @param event The MouseEvent */ ! public void mousePressed(MouseEvent event) { ! classification.show(event.getComponent(), ! event.getX(), event.getY()); ! } + + /** + * 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) { } /** + * 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 ! */ ! ! ! /** ! * Autocomplete textfield ! */ ! public class AutoCompleteField extends JTextField { ! /** The parent classification */ ! private Classification parent; + /** + * constructor of autocomplete field + * @param string is the number of colums in the field + * @param parent the parent to the classification + */ + public AutoCompleteField(String string, Classification parent) { + super(string); + this.setDocument(new AutoCompleteDocument(this, parent)); + this.setText(string); + } } + /** + * The autocomplete document + */ + public class AutoCompleteDocument extends PlainDocument { + /** The typed text */ + private String typed; + + /** The textfield */ + private JTextField field; + + /** The parent classification */ + private Classification parent; + + /** + * The AutoCompleteDocoment constructor + * @param field the Textfield + * @param parent the parent to the classification + */ + public AutoCompleteDocument(JTextField field, Classification parent) { + this.field = field; + this.parent = parent; + 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("")) { + Vector cl = parent.getChildren(); + Vector sort = new Vector(); + boolean children = false; + Iterator it = cl.iterator(); + while (it.hasNext()) { + Classification current = (Classification) it.next(); + sort.add(current.toString()); + if (current.getChildren() != null) { + children = true; + } + if (children) { + Iterator subit = current.getChildren().iterator(); + while (subit.hasNext()) { + String conc = ""; + conc = current.toString() + "." + ((Classification) subit.next()).toString(); + sort.add(conc); + } + } + } + + LinkedList matches = new LinkedList(); + Iterator sortit = sort.iterator(); + while (sortit.hasNext()) { + String name = (String) sortit.next(); + if (name.startsWith(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); + } + + } } Index: LinkAttribute.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview/LinkAttribute.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** LinkAttribute.java 20 Oct 2006 17:04:50 -0000 1.14 --- LinkAttribute.java 10 Nov 2006 13:47:41 -0000 1.15 *************** *** 251,256 **** 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); --- 251,254 ---- *************** *** 422,426 **** } }; - LinkedList sort = new LinkedList(Project.getInstance().getSpaces()); Collections.sort(sort, alpha); --- 420,423 ---- Index: ClassificationIdAttribute.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview/ClassificationIdAttribute.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ClassificationIdAttribute.java 30 Oct 2006 00:00:02 -0000 1.1 --- ClassificationIdAttribute.java 10 Nov 2006 13:47:41 -0000 1.2 *************** *** 63,67 **** label = createLabel("Classification"); component = Box.createHorizontalBox(); ! if (!attribute.getClassification().getName().equalsIgnoreCase("Classification")) { component.add(createValueLabel(attribute.getClassification().getFullId())); } --- 63,68 ---- label = createLabel("Classification"); component = Box.createHorizontalBox(); ! if (!attribute.getClassification().getName().equalsIgnoreCase("Classification") && ! attribute.getClassification().getId().intValue() != -1) { component.add(createValueLabel(attribute.getClassification().getFullId())); } |