1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui.table;
21
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.swing.AbstractButton;
27 import javax.swing.Action;
28 import javax.swing.JCheckBoxMenuItem;
29 import javax.swing.JMenu;
30 import org.xnap.commons.gui.Builder;
31 import org.xnap.commons.gui.action.AbstractToggleAction;
32 import org.xnap.commons.i18n.I18n;
33 import org.xnap.commons.i18n.I18nFactory;
34
35 /***
36 * Used for menus that control table column visibility.
37 */
38 public class TableHeaderMenu extends JMenu
39 {
40
41 private List<AbstractButton> buttons = new ArrayList<AbstractButton>();
42 private TableLayout tableLayout;
43 private TableLayoutListener layoutListener;
44 private CheckBoxHandler checkBoxListener;
45 private MaintainSortOrderAction sortOrderAction;
46
47 public TableHeaderMenu(String name, TableLayout tableLayout, int skip)
48 {
49 super(name);
50
51 layoutListener = new TableLayoutHandler();
52 checkBoxListener = new CheckBoxHandler();
53
54 setTableLayout(tableLayout);
55 }
56
57 public TableHeaderMenu(String name, TableLayout handler)
58 {
59 this(name, handler, 0);
60 }
61
62 /***
63 * Updates buttons.
64 */
65 public void updateVisibleColumns()
66 {
67
68
69
70
71
72
73
74
75
76
77 }
78
79 public void setTableLayout(TableLayout tableLayout)
80 {
81 if (this.tableLayout != null) {
82 this.tableLayout.removeTableLayoutListener(layoutListener);
83
84 for (AbstractButton button : buttons) {
85 button.removeActionListener(checkBoxListener);
86 remove(button);
87 }
88 buttons.clear();
89
90
91 }
92 this.tableLayout = tableLayout;
93 if (this.tableLayout != null) {
94 for (int i = 0; i < tableLayout.getColumnCount(); i++) {
95 JCheckBoxMenuItem checkBox = new JCheckBoxMenuItem(tableLayout.getColumnAt(i).getHeaderValue().toString());
96 checkBox.setActionCommand(Integer.toString(i));
97 checkBox.addActionListener(checkBoxListener);
98 checkBox.setSelected(tableLayout.isColumnVisible(i));
99 buttons.add(checkBox);
100 add(checkBox);
101 }
102
103 sortOrderAction = new MaintainSortOrderAction();
104 addSeparator();
105 add(Builder.createMenuItem(sortOrderAction));
106
107 updateVisibleColumns();
108 tableLayout.addTableLayoutListener(layoutListener);
109 }
110 }
111
112 private class TableLayoutHandler implements TableLayoutListener
113 {
114 public void columnLayoutChanged()
115 {
116 }
117
118 public void sortedColumnChanged()
119 {
120 }
121
122 public void columnOrderChanged()
123 {
124 }
125
126 public void columnNameChanged(int index, String newName)
127 {
128 buttons.get(index).setText(newName);
129 }
130
131 public void columnVisibilityChanged(int index, boolean visible)
132 {
133 buttons.get(index).setSelected(visible);
134 }
135
136 public void maintainSortOrderChanged(boolean newValue)
137 {
138 if (sortOrderAction != null) {
139 sortOrderAction.setSelected(newValue);
140 }
141 }
142
143 }
144
145 private class CheckBoxHandler implements ActionListener
146 {
147 public void actionPerformed(ActionEvent event)
148 {
149 JCheckBoxMenuItem checkBox = (JCheckBoxMenuItem)event.getSource();
150 if (!checkBox.isSelected() && tableLayout.getVisibleColumnsCount() == 1) {
151
152 checkBox.setSelected(true);
153 return;
154 }
155
156 int index = Integer.parseInt(checkBox.getActionCommand());
157 tableLayout.setColumnVisible(index, checkBox.isSelected());
158 }
159 }
160
161 /***
162 * Provides an action that enables or disables the maintain sort order
163 * setting of a table.
164 */
165 protected class MaintainSortOrderAction extends AbstractToggleAction
166 {
167 public MaintainSortOrderAction()
168 {
169 I18n i18n = I18nFactory.getI18n(TableHeaderMenu.class);
170 putValue(Action.NAME, i18n.tr("Maintain Sort Order"));
171 putValue(Action.SHORT_DESCRIPTION,
172 i18n.tr("If enabled, the sort order will be maintained as newitems are added to the table"));
173 }
174
175 public void toggled(boolean visible)
176 {
177 tableLayout.setMaintainSortOrder(visible);
178 }
179 }
180
181 }