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