Source code: dtk/gui/TKStandardDebatesBox.java
1 //Title: D3E ToolKit
2 //Version:
3 //Copyright: See accompanying license agreement
4 //Author: John Weatherley
5 //Company:
6 //Description: D3E Toolkit
7
8
9
10 package dtk.gui;
11
12 import dtk.core.*;
13 import java.io.*;
14 import javax.swing.*;
15 import java.util.*;
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.event.*;
19
20 /**
21 This class extends the TKAddressEntryBox to create a panel that
22 adds and deletes debate topics. It uses the same basic structure
23 as TKAddressEntryBox, only it uses the "firstName" variable to
24 store, render and edit the debate topic.
25 */
26 public class TKStandardDebatesBox extends TKAddressEntryBox implements ActionListener
27 {
28 TKWidgetMaker wm = new TKWidgetMaker();
29 JButton helpButton = wm.getHelpButton();
30
31 String variable;
32
33 /**Return the JPanel that holds this entry box.*/
34 public JPanel getPanel()
35 { return mainPanel;
36 }
37
38 /**Constructor.*/
39 public TKStandardDebatesBox()
40 {
41 }
42
43 /**Initialize and set up this entry box.*/
44 public void init(String varName,
45 String label1,
46 ParameterSet p,
47 JTextField textBox,
48 int textBoxLength,
49 JPanel _panel)
50 {
51 variable = varName;
52 params = p;
53 jLabel = new JLabel(label1);
54 label = label1;
55 panel = _panel;
56 this.textBox = textBox;
57
58 int spacer = 4;
59 Dimension s1 = new Dimension(5,5);
60 Dimension s2 = new Dimension(spacer,spacer);
61
62 this.textBoxLength = textBoxLength;
63
64 // Initialize all lists:
65 init();
66
67 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
68 setSize(mainPanel,600,buttonH * numButtons);
69
70 // Pack and render everything:
71 mainPanel.add(Box.createHorizontalGlue());
72 mainPanel.add(leftLabelBar());
73 mainPanel.add(Box.createRigidArea(s1));
74 mainPanel.add(leftButtonBar());
75 mainPanel.add(Box.createRigidArea(s1));
76 mainPanel.add(addressListScrollPane);
77 setSize(addressListScrollPane,textBoxLength - buttonW - spacer,buttonH * numButtons);
78 mainPanel.add(Box.createRigidArea(s2));
79 mainPanel.add(rightButtonBar());
80 mainPanel.setAlignmentX(mainPanel.RIGHT_ALIGNMENT);
81
82 helpButton.setToolTipText("Help on " + label);
83 helpButton.setName(variable);
84
85 updateButtons(); // Enable/disable buttons appropriately
86 }
87
88 /**Reads saved entries from the parameterSet.*/
89 public void readPersistentEntries()
90 {
91 String debates = params.get(variable,"").trim(); // Grab from file
92 Vector debatesVector = new Vector();
93
94 TKAddressBookEntry entry;
95
96 // Clear out the entry list before filling:
97 entryList.clear();
98
99 // Parse by endl:
100 StringTokenizer st1 = new StringTokenizer(debates,"\n");
101
102 // Grab all the debate topics:
103 while (st1.hasMoreTokens())
104 { debatesVector.add(st1.nextToken().trim());
105 }
106
107 // Loop through the list of names and construct the entries:
108 for(int i = 0; i < debatesVector.size(); i++)
109 {
110 entry = new TKAddressBookEntry((String)debatesVector.elementAt(i),"","");
111 entryList.add(entry);
112 }
113 }
114
115 /**
116 Register that a change has been made in the dabates entryList.
117 This ensures that the change will be displayed in the GUI the
118 next time it is visible.
119 */
120 public void registerChange()
121 {
122 Object [] entries = entryList.toArray();
123 TKAddressBookEntry entry;
124 String topics = "";
125
126 // Concat the topics:
127 for(int i = 0; i < entries.length; i++)
128 {
129 entry = (TKAddressBookEntry)entries[i];
130 topics += entry.getFirstName();
131 if(i < entries.length - 1)
132 topics += "\n";
133 }
134
135 // Register the change by saving to the (invisible) textBox:
136 textBox.setText(topics);
137 }
138
139
140 /**Creates the label bar on the left.*/
141 private JPanel leftLabelBar()
142 {
143 JPanel p = new JPanel();
144
145 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
146 p.add(jLabel);
147
148 return p;
149 }
150
151 /**Creates the button bar on the left.*/
152 private JPanel leftButtonBar()
153 {
154 JPanel p = new JPanel();
155
156 helpButton.addActionListener(new HelpButtonListener());
157
158 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
159 p.add(helpButton);
160
161 return p;
162 }
163
164 /**Listen and respond to the help buttons.*/
165 public class HelpButtonListener implements ActionListener {
166 public void actionPerformed(ActionEvent e)
167 {
168 JButton source = (JButton)(e.getSource());
169
170 // If help file exists, open the help window:
171 if(wm.fileExists(wm.getFileRootOffset() + source.getName() + ".html"))
172 {
173 helpWindow = new TKHtmlHelpWindow(null,"D3E Help",600,600,true,(source.getName() + ".html") );
174 helpWindow.show();
175 }
176 else
177 { // Otherwise, pop up a message:
178 String name = source.getName() + ".html ";
179 if(false)
180 name = "";
181
182 confirm.showMessageDialog(null,name + Constants.noHelpAvailMsg,"D3E Help",JOptionPane.PLAIN_MESSAGE);
183 }
184 }
185 }
186
187
188 /**Handle button clicking events.*/
189 public void actionPerformed(ActionEvent e)
190 {
191 // Handle menu events:
192 JButton source = (JButton)(e.getSource());
193 String buttonLabel = source.getText();
194
195
196 if(buttonLabel.equals(""))
197 buttonLabel = source.getName();
198
199 // Move the item up in the list
200 if( buttonLabel == "up")
201 {
202 moveEntry("up");
203 }
204
205 // Move the item down in the list
206 if( buttonLabel == "down")
207 {
208 moveEntry("down");
209 }
210
211 // New e-mail entry (this is disabled for now but it works)
212 if( buttonLabel == addNewEntryLabel )
213 {
214 TKDebateNewEntry newEntryDialog =
215 new TKDebateNewEntry("New " + label + " Entry",false,null,this,-1);
216 newEntryDialog.show(true);
217 }
218
219 // The edit button has been clicked with one item selected, edit it:
220 if( buttonLabel == editEntryButtonLabel )
221 {
222 editCurrentItem(addressJList.getSelectedIndex());
223 }
224
225 // Delete button
226 if( buttonLabel == removeEntryButtonLabel )
227 {
228 int ans = confirm.showOptionDialog(
229 removeEntryButton,
230 removeEntryButtonLabel + " the selected " + label + "?",
231 label + " Entry Confirmation",
232 JOptionPane.YES_NO_OPTION,
233 JOptionPane.PLAIN_MESSAGE,
234 null,null,null);
235 if(ans == JOptionPane.YES_OPTION)
236 removeSelectedItems();
237 registerChange();
238 }
239 }
240
241
242 /**
243 Pop up an entry box to allow editing of the entry.
244 */
245 protected void editCurrentItem(int index)
246 {
247 TKAddressBookEntry entry = (TKAddressBookEntry)entryList.get(index);
248
249 TKDebateNewEntry newEntryDialog =
250 new TKDebateNewEntry("Edit " + identifier + " Entry",false,entry,this,index);
251 newEntryDialog.show(true);
252 }
253
254 /**
255 Create the listModel of debate topics.
256 Override the super class by only using the firstName variable which is where
257 the debate topic is stored.
258 */
259 protected void fillListModel()
260 {
261 Object [] entriesArray = entryList.toArray();
262 TKAddressBookEntry entry;
263
264 for(int i = 0; i < entriesArray.length; i++)
265 {
266 entry = (TKAddressBookEntry)entriesArray[i];
267 listModel.addElement(entry.getFirstName());
268 }
269 }
270
271 /**Override the method so that you can save to the parameterSet when a new entry is added.*/
272 public void addNewEntry(TKAddressBookEntry entry)
273 {
274 super.addNewEntry(entry);
275 registerChange();
276 }
277
278 }