gvsig-scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.swing / org.gvsig.scripting.swing.impl / src / main / java / org / gvsig / scripting / swing / impl / composer / JHelpSelection.java @ 212
History | View | Annotate | Download (4.37 KB)
1 |
package org.gvsig.scripting.swing.impl.composer; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Dimension; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.util.HashMap; |
8 |
import java.util.Iterator; |
9 |
import java.util.Map; |
10 |
import java.util.Set; |
11 |
|
12 |
import javax.swing.DefaultListModel; |
13 |
import javax.swing.JList; |
14 |
import javax.swing.JOptionPane; |
15 |
import javax.swing.JScrollPane; |
16 |
import javax.swing.JTextField; |
17 |
import javax.swing.ListSelectionModel; |
18 |
import javax.swing.event.ListSelectionEvent; |
19 |
import javax.swing.event.ListSelectionListener; |
20 |
|
21 |
import org.gvsig.scripting.ScriptingHelpManager; |
22 |
import org.gvsig.scripting.ScriptingHelpManager.ScriptingHelpClass; |
23 |
import org.gvsig.scripting.ScriptingHelpManager.ScriptingHelpMethod; |
24 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
25 |
import org.gvsig.scripting.swing.impl.AbstractJDialogPanel; |
26 |
|
27 |
|
28 |
public class JHelpSelection extends AbstractJDialogPanel implements ActionListener{ |
29 |
|
30 |
/**
|
31 |
*
|
32 |
*
|
33 |
*/
|
34 |
private static final long serialVersionUID = -4299714728279029924L; |
35 |
|
36 |
protected JList list = null; |
37 |
protected ScriptingUIManager uimanager;
|
38 |
protected ScriptingHelpManager helpmanager;
|
39 |
protected ActionListener defaultActionlistener = null; |
40 |
protected boolean isAcceptActive = true; |
41 |
|
42 |
|
43 |
public JHelpSelection(final ScriptingUIManager uimanager,final String text){ |
44 |
this.uimanager= uimanager;
|
45 |
this.helpmanager = uimanager.getManager().getHelpManager();
|
46 |
|
47 |
Map<String, ScriptingHelpMethod> methods = |
48 |
uimanager.getManager().getHelpManager().findMethods(text); |
49 |
final HashMap<String, String> helpReferences = new HashMap<String, String>(); |
50 |
|
51 |
ListSelectionListener menuListener = new ListSelectionListener() { |
52 |
public void valueChanged(ListSelectionEvent arg0) { |
53 |
if(arg0.getValueIsAdjusting()){
|
54 |
String[] s = ((JList)arg0.getSource()).getSelectedValue().toString().split(" - "); |
55 |
String selection = ""; |
56 |
if(s.length==1){ |
57 |
selection = s[0];
|
58 |
}else{
|
59 |
for(int i=0;i<s.length-1;i++){ |
60 |
selection=selection+s[i]; |
61 |
} |
62 |
} |
63 |
launchException(this, 1, helpReferences.get(selection)); |
64 |
} |
65 |
} |
66 |
|
67 |
}; |
68 |
DefaultListModel listModel = new DefaultListModel(); |
69 |
|
70 |
for(int x=0;x<methods.size();x++){ |
71 |
Set<String> keys = methods.keySet(); |
72 |
Object[] l = keys.toArray(); |
73 |
|
74 |
ScriptingHelpMethod helpMethod = methods.get(l[x]); |
75 |
Iterator<ScriptingHelpClass> it = helpMethod.iterator();
|
76 |
while(it.hasNext()){
|
77 |
ScriptingHelpClass cn = it.next(); |
78 |
if(!(helpReferences.containsKey(helpMethod.getName()) && helpReferences.containsValue(cn.getName()))){
|
79 |
helpReferences.put(cn.getName(),cn.getUrl()); |
80 |
} |
81 |
} |
82 |
} |
83 |
|
84 |
setLayout(new BorderLayout()); |
85 |
|
86 |
|
87 |
for(int x=0;x<helpReferences.size();x++){ |
88 |
Set<String> keys = helpReferences.keySet(); |
89 |
Object[] l = keys.toArray(); |
90 |
|
91 |
listModel.addElement(l[x].toString()); |
92 |
} |
93 |
if(listModel.getSize()!=0){ |
94 |
list = new JList(listModel); |
95 |
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
96 |
list.setLayoutOrientation(JList.VERTICAL);
|
97 |
list.setVisibleRowCount(0);
|
98 |
list.addListSelectionListener(menuListener); |
99 |
JScrollPane scroll = new JScrollPane(list); |
100 |
|
101 |
add(scroll,BorderLayout.CENTER);
|
102 |
setPreferredSize(new Dimension(300,150)); |
103 |
}else{
|
104 |
JTextField textfield = new JTextField("There isn't any help to the text '"+text+"'."); |
105 |
textfield.setEditable(false);
|
106 |
add(textfield,BorderLayout.CENTER);
|
107 |
setPreferredSize(new Dimension(300,50)); |
108 |
this.isAcceptActive=false; |
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
@Override
|
114 |
public void actionAccept() { |
115 |
int selected = list.getSelectedIndex();
|
116 |
if(selected==-1){ |
117 |
JOptionPane.showMessageDialog(null, "Please, select any help", "Error", JOptionPane.ERROR_MESSAGE); |
118 |
}else{
|
119 |
setVisible(false);
|
120 |
} |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void actionApply() { |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public void actionCancel() { |
129 |
setVisible(false);
|
130 |
} |
131 |
|
132 |
@Override
|
133 |
public boolean isVisibleAccept() { |
134 |
return this.isAcceptActive; |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public boolean isVisibleApply() { |
139 |
return false; |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public boolean isVisibleCancel() { |
144 |
return true; |
145 |
} |
146 |
|
147 |
private void launchException(Object source, int id, String action){ |
148 |
this.defaultActionlistener.actionPerformed(
|
149 |
new ActionEvent(source,id,action) |
150 |
); |
151 |
} |
152 |
|
153 |
public void actionPerformed(ActionEvent arg0) { |
154 |
|
155 |
} |
156 |
|
157 |
public void addDefaultActionListener(ActionListener actionlistener) { |
158 |
this.defaultActionlistener = actionlistener;
|
159 |
} |
160 |
|
161 |
} |