[Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/attrview LinkAttribute.java,1.6,1.7
Status: Pre-Alpha
Brought to you by:
henryml
|
From: Nikolaj B. <nbr...@us...> - 2006-05-15 09:20:27
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv22936/src/net/sourceforge/bprocessor/gui/attrview Modified Files: LinkAttribute.java Log Message: The textFields now autocomplete. Index: LinkAttribute.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview/LinkAttribute.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** LinkAttribute.java 24 Apr 2006 08:45:10 -0000 1.6 --- LinkAttribute.java 15 May 2006 09:20:23 -0000 1.7 *************** *** 15,18 **** --- 15,19 ---- import java.awt.event.MouseListener; import java.net.URL; + import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; *************** *** 25,37 **** 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 */ --- 26,45 ---- 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.Project; import net.sourceforge.bprocessor.model.Selection; + import net.sourceforge.bprocessor.model.Space; /** * The LinkAttributeView */ ! public class LinkAttribute extends GenericAttribute ! implements KeyListener, MouseListener { /** The listeners */ *************** *** 51,55 **** /** The editor, when this EditableAttribute is being edited */ ! private JTextField editor; --- 59,63 ---- /** The editor, when this EditableAttribute is being edited */ ! private AutoCompleteField editor; *************** *** 164,168 **** JLabel valueLabel; if (value instanceof Entity) { ! s = ((Entity)value).getName(); valueLabel = new JLabel(s); } else { --- 172,176 ---- JLabel valueLabel; if (value instanceof Entity) { ! s = ((Entity)value).getName() + "-" + ((Entity)value).getId().toString(); valueLabel = new JLabel(s); } else { *************** *** 196,200 **** s = Double.toString(rounded); } ! JTextField valueEditor = new JTextField(s); valueEditor.setFont(AttributeView.FONT_PLAIN); --- 204,208 ---- s = Double.toString(rounded); } ! AutoCompleteField valueEditor = new AutoCompleteField(s); valueEditor.setFont(AttributeView.FONT_PLAIN); *************** *** 218,222 **** if (editor == null) { component.remove(0); ! editor = (JTextField) createValueEditor(attribute.getValue()); editor.addKeyListener(this); component.add(editor); --- 226,230 ---- if (editor == null) { component.remove(0); ! editor = (AutoCompleteField) createValueEditor(attribute.getValue()); editor.addKeyListener(this); component.add(editor); *************** *** 263,266 **** --- 271,276 ---- } + + /** * Respond to the enter key by stopping editing. *************** *** 272,276 **** } else if (event.getKeyCode() == KeyEvent.VK_ESCAPE) { cancelEditing(); ! } } --- 282,286 ---- } else if (event.getKeyCode() == KeyEvent.VK_ESCAPE) { cancelEditing(); ! } } *************** *** 314,316 **** --- 324,430 ---- */ 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("")) { + List sort = new LinkedList(Project.getInstance().getSpaces()); + Collections.sort((List) sort); + LinkedList matches = new LinkedList(); + Iterator it = sort.iterator(); + while (it.hasNext()) { + Space current = (Space) 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); + } + } } |