Repository navigation
Expand file tree
/
Copy pathgui.java
More file actions
369 lines (298 loc) · 12.5 KB
/
Copy pathgui.java
File metadata and controls
369 lines (298 loc) · 12.5 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.logging.*;
import javax.swing.JButton;
import javax.swing.JPopupMenu;
import javax.swing.JPanel;
/** Class gui. Creates the window with file-menus and buttons
* @author Jon Arne Westgaard
*/
public class gui extends JFrame {
private File fileLoadTable = null;
private Logger logger = Logger.getLogger("MyLog"); // Logging mechanism
final tablemodel t2; // Our table model used in the system
final JTable table1; // The JTable for use in system
public gui() {
super (prosjekt1.getMessages().getString("title"));
guiEventHandlers evtHandle = new guiEventHandlers();
//TODO: Decide if this should last
t2 = new tablemodel();
table1 = new JTable(t2);
// Menubar
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
// Filemenu
JMenu fileMenu = new JMenu (prosjekt1.getMessages().getString("file"));
fileMenu.setMnemonic('F');
fileMenu.setToolTipText(prosjekt1.getMessages().getString("filettt"));
// Filemenu items
JMenuItem newItem = new JMenuItem(prosjekt1.getMessages().getString("new"), new ImageIcon("icons/NEW.GIF"));
newItem.setMnemonic('N');
newItem.setToolTipText(prosjekt1.getMessages().getString("newttt"));
// ActionListener:
newItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
t2.ny();
gui.this.fileLoadTable = null;
}}); // Creates new file, add listener
fileMenu.add(newItem);
JMenuItem loadItem = new JMenuItem(prosjekt1.getMessages().getString("load"), new ImageIcon("icons/OPENDOC.GIF"));
loadItem.setMnemonic('L');
loadItem.setToolTipText(prosjekt1.getMessages().getString("loadttt"));
// ActionListener:
loadItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
gui.this.openFileDialog();
}}); // Loads new file, add listener
fileMenu.add(loadItem);
JMenuItem saveItem = new JMenuItem(prosjekt1.getMessages().getString("save"), new ImageIcon("icons/SAVE.GIF"));
saveItem.setMnemonic('S');
saveItem.setToolTipText(prosjekt1.getMessages().getString("savettt"));
// ActionListener:
saveItem.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent evt){
gui.this.saveFileDialog();
}
});
fileMenu.add(saveItem);
JMenuItem saveasItem = new JMenuItem(prosjekt1.getMessages().getString("saveas"), new ImageIcon("icons/SAVEJAVA.GIF"));
saveasItem.setMnemonic(KeyEvent.VK_A);
saveasItem.setToolTipText(prosjekt1.getMessages().getString("saveasttt"));
fileMenu.add(saveasItem);
// ActionListener
saveasItem.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent evt){
gui.this.saveFileDialog();
}
});
fileMenu.addSeparator();
JMenuItem previewItem = new JMenuItem(prosjekt1.getMessages().getString("preview"));
previewItem.setMnemonic('P');
previewItem.setToolTipText(prosjekt1.getMessages().getString("previewttt"));
fileMenu.add(previewItem);
// ActionListener:
saveItem.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent evt){
// gui.this.saveFileDialog();
}
});
JMenuItem generateItem = new JMenuItem(prosjekt1.getMessages().getString("genjava"), new ImageIcon("icons/SAVEJAVA.GIF"));
generateItem.setMnemonic(KeyEvent.VK_J);
generateItem.setToolTipText(prosjekt1.getMessages().getString("genjavattt"));
fileMenu.add(generateItem);
// ActionListener:
saveItem.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent evt){
gui.this.saveFileDialog();
}
});
fileMenu.addSeparator();
JMenuItem exitItem = new JMenuItem(prosjekt1.getMessages().getString("exit"));
exitItem.setMnemonic('E');
exitItem.setToolTipText(prosjekt1.getMessages().getString("exitttt"));
// ActionListener:
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
int answer = JOptionPane.showConfirmDialog(gui.this, prosjekt1.getMessages().getString("msgQuitConfirmation"),prosjekt1.getMessages().getString("title"), JOptionPane.YES_NO_CANCEL_OPTION);
if(answer == 1) // If user wants to save changes
// TODO: Insert Save code
if(answer != 2) // If user doesn't press cancel
System.exit(NORMAL); // Normal exit event
}
});
fileMenu.add(exitItem);
bar.add(fileMenu);
// Editmenu
JMenu editMenu = new JMenu (prosjekt1.getMessages().getString("edit"));
editMenu.setMnemonic('E');
editMenu.setToolTipText(prosjekt1.getMessages().getString("editttt"));
// Editmenu items
JMenuItem newrowItem = new JMenuItem(prosjekt1.getMessages().getString("newrow"), new ImageIcon("icons/NEWROW.GIF"));
newrowItem.setToolTipText(prosjekt1.getMessages().getString("newrowttt"));
editMenu.add(newrowItem);
// ActionListener:
newrowItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
t2.insertRow();
}
});
editMenu.addSeparator();
JMenuItem preferencesItem = new JMenuItem(prosjekt1.getMessages().getString("preferences"));
preferencesItem.setMnemonic('P');
preferencesItem.setToolTipText(prosjekt1.getMessages().getString("preferencesttt"));
// ActionListener:
preferencesItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
new guiPrefs();
}
});
editMenu.add(preferencesItem);
bar.add(editMenu);
// Helpmenu
JMenu helpMenu = new JMenu (prosjekt1.getMessages().getString("help"));
helpMenu.setMnemonic('H');
helpMenu.setToolTipText(prosjekt1.getMessages().getString("helpttt"));
// Helpmenu items
JMenuItem helpItem = new JMenuItem(prosjekt1.getMessages().getString("help"), new ImageIcon("icons/HELP.GIF"));
helpItem.setMnemonic('H');
helpItem.setToolTipText(prosjekt1.getMessages().getString("helpttt"));
helpMenu.add(helpItem);
// ActionListener:
helpItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
JOptionPane.showMessageDialog(gui.this,"Fukcing google it!","Please...",JOptionPane.INFORMATION_MESSAGE);
}
});
helpMenu.addSeparator();
JMenuItem aboutItem = new JMenuItem(prosjekt1.getMessages().getString("about"));
aboutItem.setMnemonic('A');
aboutItem.setToolTipText(prosjekt1.getMessages().getString("aboutttt"));
aboutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
JOptionPane.showMessageDialog(null, "Created by Christer Vaskinn and Jon Arne Westgaard","About",JOptionPane.INFORMATION_MESSAGE);
}
});
helpMenu.add(aboutItem);
bar.add(helpMenu);
// Menubar
JToolBar ikoner = new JToolBar ();
JButton newButton = new JButton(new ImageIcon("icons/NEW.GIF"));
newButton.setToolTipText(prosjekt1.getMessages().getString("new"));
JButton loadButton = new JButton(new ImageIcon("icons/OPENDOC.GIF"));
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
gui.this.openFileDialog();
}}); // Loads new file, add listener
loadButton.setToolTipText(prosjekt1.getMessages().getString("loadttt"));
// Actionlistener:
newButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
t2.ny();
gui.this.fileLoadTable = null;
}
});
JButton saveButton = new JButton(new ImageIcon("icons/SAVE.GIF"));
saveButton.setToolTipText(prosjekt1.getMessages().getString("savettt"));
// Actionlistener:
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
gui.this.saveFileDialog();
}
});
JButton previewButton = new JButton(new ImageIcon("icons/ExecuteProject.gif"));
previewButton.setToolTipText(prosjekt1.getMessages().getString("previewttt"));
// Actionlistener:
newButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
t2.ny();
gui.this.fileLoadTable = null;
}
});
JButton generateButton = new JButton(new ImageIcon("icons/SAVEJAVA.GIF"));
generateButton.setToolTipText(prosjekt1.getMessages().getString("previewttt"));
// Actionlistener:
newButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
t2.ny();
gui.this.fileLoadTable = null;
}
});
JButton newrowButton = new JButton(new ImageIcon("icons/NEWROW.GIF"));
newrowButton.setToolTipText(prosjekt1.getMessages().getString("newrowttt"));
// Actionlistener:
newrowButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
t2.insertRow();
}
});
JButton moveupButton = new JButton(new ImageIcon("icons/MoveRowUp.gif"));
moveupButton.setToolTipText(prosjekt1.getMessages().getString("moveupttt"));
// Actionlistener:
moveupButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// Need to check if row is already at the top
t2.moverowup(table1.getSelectedRow());
}
});
JButton movedownButton = new JButton(new ImageIcon("icons/MoveRowDown.gif"));
movedownButton.setToolTipText(prosjekt1.getMessages().getString("movedownttt"));
// Actionlistener:
movedownButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// Need to check if row is already at the bottom
t2.moverowdown(table1.getSelectedRow());
}
});
JButton aboutButton = new JButton(new ImageIcon("icons/HELP.GIF"));
aboutButton.setToolTipText(prosjekt1.getMessages().getString("helpttt"));
aboutButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
JOptionPane.showMessageDialog(null, "Created by Christer Vaskinn and Jon Arne Westgaard","About",JOptionPane.INFORMATION_MESSAGE);
}
});
ikoner.add(newButton);
ikoner.add(loadButton);
ikoner.add(saveButton);
ikoner.add(previewButton);
ikoner.add(generateButton);
ikoner.add(newrowButton);
ikoner.add(moveupButton);
ikoner.add(movedownButton);
ikoner.add(aboutButton);
add(ikoner, BorderLayout.NORTH);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table1);
//Add the scroll pane to this panel.
add(scrollPane);
pack();
setVisible (true);
}
private void openFileDialog() {
try {
JFileChooser fileChooser = new JFileChooser(".");
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // Sets mode of the filechooser
int result = fileChooser.showOpenDialog(this); // Opens file dialog
this.fileLoadTable = fileChooser.getSelectedFile();
if(fileLoadTable == null || result == JFileChooser.CANCEL_OPTION){
return;
}
RandomAccessFile randFile = new RandomAccessFile(this.fileLoadTable, "r");
this.t2.openTableFile(randFile);
setTitle(prosjekt1.getMessages().getString("title") + this.fileLoadTable.getName()); // Sets title of main window
}
catch( Exception e){
logger.log(Level.WARNING,"Error e: " + e.getMessage());
}
}
private void saveFileDialog(){
try
{
if ((this.fileLoadTable == null)) {
JFileChooser fileChooser = new JFileChooser(".");
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.showSaveDialog(this);
this.fileLoadTable = fileChooser.getSelectedFile();
setTitle(prosjekt1.getMessages().getString("title") + this.fileLoadTable.getName());
}
Object objLocalFile = new RandomAccessFile(this.fileLoadTable, "rw"); // Generate file
this.t2.saveToFile((RandomAccessFile)objLocalFile); // Write data to file
((RandomAccessFile)objLocalFile).close();
} catch (Exception e) {
logger.log(Level.WARNING,"Feil under lagring til fil : " + e.toString());
}
}
}