1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /*****************************************************************************
25 * HashtablePanel.java
26 *
27 *
28 ***************************************************************************/
29
30 package zeus.generator.util;
31
32 import java.util.*;
33 import java.awt.*;
34 import java.awt.event.*;
35 import javax.swing.*;
36 import javax.swing.border.*;
37 import javax.swing.table.*;
38 import javax.swing.event.*;
39
40 import zeus.util.*;
41 import zeus.gui.help.*;
42
43 public class HashtablePanel extends JPanel {
44 static final String[] ERROR_MESSAGE = {
45
46 };
47
48 protected HashtableModel model;
49 protected JTable table;
50
51 public HashtablePanel(HashtableModel model) {
52 this.model = model;
53
54 TableColumnModel tm = new DefaultTableColumnModel();
55 TableColumn column;
56
57 column = new TableColumn(HashtableModel.KEY,12);
58 column.setHeaderValue(model.getColumnName(HashtableModel.KEY));
59 tm.addColumn(column);
60
61 column = new TableColumn(HashtableModel.VALUE,24);
62 column.setHeaderValue(model.getColumnName(HashtableModel.VALUE));
63 tm.addColumn(column);
64
65 table = new JTable(model,tm);
66 table.getTableHeader().setReorderingAllowed(false);
67 table.setColumnSelectionAllowed(false);
68
69 TitledBorder border = BorderFactory.createTitledBorder("Key-Value Table");
70 border.setTitlePosition(TitledBorder.TOP);
71 border.setTitleJustification(TitledBorder.RIGHT);
72 border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
73 border.setTitleColor(Color.blue);
74 setBorder(border);
75
76 JScrollPane scrollPane = new JScrollPane(table);
77 scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
78 scrollPane.setMinimumSize(new Dimension(160,80));
79 scrollPane.setPreferredSize(new Dimension(200,80));
80 table.setBackground(Color.white);
81
82 GridBagLayout gridBagLayout = new GridBagLayout();
83 setLayout(gridBagLayout);
84 GridBagConstraints gbc;
85 setBackground(Color.lightGray);
86
87 JToolBar toolbar = new HashtableToolBar();
88 gbc = new GridBagConstraints();
89 gbc.anchor = GridBagConstraints.NORTHWEST;
90 gbc.fill = GridBagConstraints.NONE;
91 gbc.gridwidth = GridBagConstraints.REMAINDER;
92 gbc.insets = new Insets(0,8,0,0);
93 gridBagLayout.setConstraints(toolbar,gbc);
94 add(toolbar);
95
96 gbc = new GridBagConstraints();
97 gbc.gridwidth = GridBagConstraints.REMAINDER;
98 gbc.anchor = GridBagConstraints.NORTHWEST;
99 gbc.fill = GridBagConstraints.BOTH;
100 gbc.weightx = gbc.weighty = 1;
101 gbc.insets = new Insets(8,8,8,8);
102 gridBagLayout.setConstraints(scrollPane,gbc);
103 add(scrollPane);
104 }
105
106 protected class HashtableToolBar extends JToolBar
107 implements ActionListener {
108
109 protected JToggleButton helpBtn;
110 protected HelpWindow helpWin;
111 protected JButton newBtn;
112 protected JButton deleteBtn;
113
114 public HashtableToolBar() {
115 setBackground(Color.lightGray);
116 setBorder( new BevelBorder(BevelBorder.LOWERED ) );
117 setFloatable(false);
118 String path = SystemProps.getProperty("gif.dir") + "generator" +
119 System.getProperty("file.separator");
120
121 newBtn = new JButton(new ImageIcon(path + "new1.gif"));
122 newBtn.setMargin(new Insets(0,0,0,0));
123 add(newBtn);
124 newBtn.setToolTipText("New");
125 newBtn.addActionListener(this);
126
127 deleteBtn = new JButton(new ImageIcon(path + "delete1.gif"));
128 deleteBtn.setMargin(new Insets(0,0,0,0));
129 add(deleteBtn);
130 deleteBtn.setToolTipText("Delete");
131 deleteBtn.addActionListener(this);
132
133
134 helpBtn = new JToggleButton(new ImageIcon(path + "help.gif"));
135 helpBtn.setMargin(new Insets(0,0,0,0));
136 add(helpBtn);
137 helpBtn.setToolTipText("Help");
138 helpBtn.addActionListener(this);
139 }
140
141 public void actionPerformed(ActionEvent e) {
142 Object src = e.getSource();
143 if ( src == newBtn ) {
144 model.addNewRow();
145 repaint();}
146 else if ( src == deleteBtn ) {
147 if ( isRowSelected() )
148 model.removeRows(table.getSelectedRows());
149 repaint();
150 }
151 else if ( src == helpBtn ) {
152 if ( helpBtn.isSelected() ) {
153 helpWin = new HelpWindow(SwingUtilities.getRoot(this),
154 getLocation(), "generator", "Generation Plan: Tasks");
155 helpWin.setSource(helpBtn);
156 }
157 else {
158 helpWin.dispose();
159 }
160 repaint();
161 }
162 }
163 }
164
165 protected boolean isRowSelected() {
166 int row = table.getSelectedRow();
167 if ( row == -1) {
168 errorMsg(0);
169 return false;
170 }
171 return true;
172 }
173
174 protected void errorMsg(int tag) {
175 JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
176 "Error", JOptionPane.ERROR_MESSAGE);
177 }
178
179 public void reset(Hashtable input) {
180 model.reset(input);
181 }
182
183 public Hashtable getData() {
184 return model.getData();
185 }
186 }