-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit Algo modif and Graph by Diego
- Loading branch information
Showing
76 changed files
with
671 additions
and
11 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package algo.graph.graphic; | ||
|
||
|
||
import java.awt.Color; | ||
import java.awt.Dimension; | ||
import java.awt.FlowLayout; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import javax.swing.BorderFactory; | ||
import javax.swing.JFrame; | ||
import javax.swing.JLabel; | ||
import javax.swing.JTextField; | ||
import javax.swing.text.AttributeSet; | ||
import javax.swing.text.BadLocationException; | ||
import javax.swing.text.JTextComponent; | ||
import javax.swing.text.PlainDocument; | ||
|
||
import algo.graph.parsing.Stops; | ||
|
||
public class AutoCompleteDocument extends PlainDocument | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
private List<String> dictionary = new ArrayList<String>(); | ||
|
||
private final JTextComponent _textField; | ||
|
||
public AutoCompleteDocument(JTextComponent field, List<Stops> aDictionary) | ||
{ | ||
_textField = field; | ||
|
||
for(Stops stop : aDictionary) | ||
{ | ||
dictionary.add(stop.stop_name); | ||
} | ||
} | ||
|
||
public void addDictionaryEntry(String item) | ||
{ | ||
dictionary.add(item); | ||
} | ||
|
||
@Override | ||
public void insertString(int offs, String str, AttributeSet a) | ||
throws BadLocationException | ||
{ | ||
super.insertString(offs, str, a); | ||
String word = autoComplete(getText(0, getLength())); | ||
if (word != null) { | ||
super.insertString(offs + str.length(), word, a); | ||
_textField.setCaretPosition(offs + str.length()); | ||
_textField.moveCaretPosition(getLength()); | ||
} | ||
} | ||
|
||
public String autoComplete(String text) { | ||
for (Iterator<String> i = dictionary.iterator(); i.hasNext();) { | ||
String word = i.next(); | ||
if (word.startsWith(text)) { | ||
return word.substring(text.length()); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
public static JTextField createAutoCompleteTextField(List<Stops> dictionary) | ||
{ | ||
JTextField field = new JTextField(20); | ||
field.setPreferredSize(new Dimension(180, 20)); | ||
field.setBorder(BorderFactory.createLineBorder(Color.BLACK)); | ||
|
||
AutoCompleteDocument doc = new AutoCompleteDocument(field, dictionary); | ||
field.setDocument(doc); | ||
return field; | ||
} | ||
|
||
/*public static void main(String[] args) | ||
{ | ||
//String[] dict = { "Alef++", "alef++", "sourceForge", "SourceFORGE", "JAVA","JAVA2","PROGRAMMATION", "programmation", "Team" }; | ||
JTextField field = AutoCompleteDocument.createAutoCompleteTextField(dict); | ||
JFrame frame = new JFrame("Autocomplete"); | ||
frame.setLayout(new FlowLayout()); | ||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
frame.add(new JLabel("Text Field: ")); | ||
frame.add(field); | ||
frame.pack(); | ||
frame.setVisible(true); | ||
}*/ | ||
} | ||
|
Oops, something went wrong.