1 /*
2 J-Bird net/sourceforge/jbird/SpeciesTableDialog.java
3
4 Copyright 2001, 2002, 2003 Dick Repasky
5 */
6 package net.sourceforge.jbird;
7
8 import java.util.ResourceBundle;
9
10 import java.awt.Dialog;
11 import java.awt.Frame;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14
15 import javax.swing.ButtonGroup;
16 import javax.swing.JMenu;
17 import javax.swing.JMenuItem;
18 import javax.swing.JRadioButtonMenuItem;
19 import javax.swing.JScrollPane;
20
21 import net.sourceforge.jbird.swing.SavableTableDialog;
22
23 /**
24 * A dialog for presenting species tables. The dialog provides
25 * a View menu that can be used to toggle between common and scientific
26 * names, show/hide families, and allow families to be collapsed.
27 *
28 * @author Dick Repasky
29 * @since J-Bird 0.1.3
30 *
31 */
32
33 public class SpeciesTableDialog extends SavableTableDialog {
34
35 protected SpeciesTableModel table_model;
36
37 protected JMenu view_menu;
38 protected JRadioButtonMenuItem common_names;
39 protected JRadioButtonMenuItem scientific_names;
40 protected JRadioButtonMenuItem show_families;
41 protected JRadioButtonMenuItem collapsable_families;
42
43 /**
44 * A minimal constructor that can be used only by
45 * subclasses. Add comments for how to
46 * configure, methods, too!
47 * @since J-Bird 0.3.0
48 */
49
50 protected SpeciesTableDialog(Frame owner) {
51 super(owner);
52 buildViewMenu();
53 }
54
55 /**
56 * A minimal constructor that can be used only by
57 * subclasses. Add comments for how to
58 * configure, methods, too!
59 * @since J-Bird 0.3.0
60 */
61
62 protected SpeciesTableDialog(Dialog owner) {
63 super(owner);
64 buildViewMenu();
65 }
66
67 public SpeciesTableDialog(Frame owner, SpeciesTable table) {
68 super(owner, table);
69 table_model = table.getSpeciesTableModel();
70 buildViewMenu();
71 }
72
73 public SpeciesTableDialog(Dialog owner, SpeciesTable table) {
74 super(owner, table);
75 table_model = table.getSpeciesTableModel();
76 buildViewMenu();
77 }
78
79 /** Useful if JScrollPane already built. */
80 public SpeciesTableDialog(Frame owner, JScrollPane scrollpane,
81 SpeciesTable table) {
82 super(owner, scrollpane, table);
83 table_model = table.getSpeciesTableModel();
84 buildViewMenu();
85 }
86
87 /** Useful if JScrollPane already built. */
88 public SpeciesTableDialog(Dialog owner, JScrollPane scrollpane,
89 SpeciesTable table) {
90 super(owner, scrollpane, table);
91 table_model = table.getSpeciesTableModel();
92 buildViewMenu();
93 }
94
95 public final void localize(ResourceBundle progres) {
96 view_menu.setText(progres.getString("View"));
97 collapsable_families.setText(
98 progres.getString("Families_collapsable"));
99 common_names.setText(progres.getString("Common_names"));
100 scientific_names.setText(progres.getString("Scientific_names"));
101 show_families.setText(progres.getString("Show_families"));
102 }
103
104 public void nullify() {
105 table_model.nullify();
106 table_model = null;
107 view_menu = null;
108 common_names = null;
109 scientific_names = null;
110 show_families = null;
111 collapsable_families = null;
112 super.nullify();
113 }
114
115 /////////////////// protected below /////////////////////////
116
117 /** Build the view menu. */
118 protected final void buildViewMenu() {
119 view_menu = new JMenu("View");
120 buildCommonScientific();
121 view_menu.add(new JMenuItem(" ")); // spacer
122 buildFamilies();
123 view_menu.add(new JMenuItem(" ")); // spacer
124 buildCollapsable();
125 menu_bar.add(view_menu);
126 setViewSelections();
127 }
128
129 /** Build the collapsable portion of View menu. */
130 protected final void buildCollapsable() {
131 collapsable_families = new JRadioButtonMenuItem("Families collapsable");
132 view_menu.add(collapsable_families);
133 class CollapsableListener implements ActionListener {
134 public void actionPerformed(ActionEvent e) {
135 toggleCollapsable();
136 }
137 }
138 collapsable_families.addActionListener(new CollapsableListener());
139 }
140
141 /** Build the common/scientific portion of View menu. */
142 protected final void buildCommonScientific() {
143 common_names = new JRadioButtonMenuItem("Common names");
144 scientific_names = new JRadioButtonMenuItem("Scientific names");
145 ButtonGroup namegroup = new ButtonGroup();
146 namegroup.add(common_names);
147 namegroup.add(scientific_names);
148 view_menu.add(common_names);
149 view_menu.add(scientific_names);
150
151 common_names.setActionCommand("common");
152 scientific_names.setActionCommand("scientific");
153
154 class NameListener implements ActionListener {
155 public void actionPerformed(ActionEvent e) {
156 toggleNames(e);
157 }
158 }
159 NameListener nl = new NameListener();
160 common_names.addActionListener(nl);
161 scientific_names.addActionListener(nl);
162 }
163
164 /** Build the families portion of View menu. */
165 protected final void buildFamilies() {
166 show_families = new JRadioButtonMenuItem("Show families");
167 view_menu.add(show_families);
168 class FamiliesListener implements ActionListener {
169 public void actionPerformed(ActionEvent e) {
170 toggleFamilies();
171 }
172 }
173 show_families.addActionListener(new FamiliesListener());
174 }
175
176 /**
177 * Modify state of View menu items to match the state
178 * of the table model.
179 * @since J-Bird 0.3.0
180 */
181 protected final void setViewSelections() {
182 if (table_model != null) {
183 if (table_model.isShowScientific()) {
184 scientific_names.setSelected(true);
185 } else {
186 common_names.setSelected(true);
187 }
188 show_families.setSelected(table_model.isShowFamilies());
189 collapsable_families.setSelected(
190 table_model.isCollapsable());
191 }
192 }
193
194 /** Pass request to allow/disallow collapsing families to
195 the table model. */
196 protected final void toggleCollapsable() {
197 table_model.setCollapsable(collapsable_families.isSelected());
198 }
199
200 /** Pass request to show/hide families to the table model. */
201 protected final void toggleFamilies() {
202 table_model.setShowFamilies(show_families.isSelected());
203 }
204
205 /** Toggle between scientific names and common names. */
206 protected final void toggleNames(ActionEvent e) {
207 String cmd = e.getActionCommand();
208 boolean showscientific = true;
209 if (cmd.equals("common")) {
210 showscientific = false;
211 }
212 table_model.setShowScientific(showscientific);
213 }
214
215 /** Overrides SavableTableDialog. Sets bold to a desireable
216 value and then invokes parent. */
217 protected void saveAsCSV() {
218 int bold = table_model.getBoldness();
219 table_model.setBoldness(SpeciesTableModel.NO_BOLD);
220 super.saveAsCSV();
221 table_model.setBoldness(bold);
222 }
223
224 /** Overrides SavableTableDialog. Sets bold to a desireable
225 value and then invokes parent. */
226 protected void saveAsHTML() {
227 int bold = table_model.getBoldness();
228 table_model.setBoldness(SpeciesTableModel.BOLD_EXPORT);
229 super.saveAsHTML();
230 table_model.setBoldness(bold);
231 }
232
233 }
234