Subversion Repositories public

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
85 luk 1
/*
2
 * ICFrame.java - jIncron application GUI frame
3
 *
4
 * Copyright (c) 2006 Lukas Jelinek, http://www.aiken.cz
5
 *
6
 * ==========================================================================
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License Version 2 as
10
 * published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 *
21
 * ==========================================================================
22
 */
23
 
24
package cz.aiken.util.jincron;
25
 
26
import java.awt.*;
27
import java.awt.event.*;
28
import java.io.*;
29
import javax.swing.*;
30
import javax.swing.event.*;
31
import javax.swing.table.*;
32
 
33
/**
34
 *
35
 * @author luk
36
 */
37
public class ICFrame extends JFrame implements ActionListener, ListSelectionListener {
38
 
39
    private static final String pgmName = "jIncron";
40
 
41
    private JMenuBar menubar = new JMenuBar();
42
    private JMenuItem editRowItem = null;
43
    private JMenuItem removeRowItem = null;
44
 
45
    private JButton addBut = new JButton("Add...");
46
    private JButton editBut = new JButton("Edit...");
47
    private JButton removeBut = new JButton("Remove");
48
 
49
    private JPanel butPanel = new JPanel();
50
    private ICTableModel model = new ICTableModel();
51
    private JTable table = null;
52
 
53
    private boolean changed = false;
54
 
55
    /** Creates a new instance of ICFrame */
56
    public ICFrame() {
57
        super();
58
        init();
59
    }
60
 
61
    protected void init() {
62
        setSize(ICConfig.getWindowSize());
63
        setLocation(ICConfig.getWindowLocation());
64
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
65
        updateTitle();
66
 
67
        initMenus();
68
 
69
        initButtons();
70
 
71
        table = new JTable(model);
72
        table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
73
        table.getSelectionModel().addListSelectionListener(this);
74
        JScrollPane jsp = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
75
        JSplitPane spl = new JSplitPane(JSplitPane.VERTICAL_SPLIT, butPanel, jsp);
76
        setContentPane(spl);
77
 
78
        load();
79
 
80
        updateMenus();
81
        updateButtons();
82
    }
83
 
84
    protected void initButtons() {
85
        butPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
86
        initButton(addBut, "add", "Add a new rule");
87
        initButton(editBut, "edit", "Edit the selected rule");
88
        initButton(removeBut, "remove", "Remove the selected rule(s)");
89
    }
90
 
91
    protected void initButton(JButton but, String cmd, String tooltip) {
92
        but.setActionCommand(cmd);
93
        but.addActionListener(this);
94
        but.setToolTipText(tooltip);
95
        butPanel.add(but);
96
    }
97
 
98
    protected void initMenus() {
99
        JMenu menu = new JMenu("File");
100
        initMenuItem("New", 'n', KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK), "new", this, menu);
101
        initMenuItem("Load", 'l', KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK), "load", this, menu);
102
        initMenuItem("Save", 's', KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK), "save", this, menu);
103
        menu.addSeparator();
104
        initMenuItem("Import...", 'i', KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK), "import", this, menu);
105
        initMenuItem("Export...", 'x', KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK), "export", this, menu);
106
        menu.addSeparator();
107
        initMenuItem("Quit", 'q', KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.ALT_MASK), "quit", this, menu);
108
        menubar.add(menu);
109
 
110
        menu = new JMenu("Action");
111
        initMenuItem("Add...", 'a', KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK), "add", this, menu);
112
        editRowItem = initMenuItem("Edit...", 'e', KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK), "edit", this, menu);
113
        removeRowItem = initMenuItem("Remove", 'v', KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK), "remove", this, menu);
114
        menubar.add(menu);
115
 
116
        menu = new JMenu("Help");
117
        initMenuItem("About", 'b', KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK), "about", this, menu);
118
        menubar.add(menu);
119
 
120
        setJMenuBar(menubar);
121
    }
122
 
123
    protected JMenuItem initMenuItem(String text, int mnemo, KeyStroke accel, String cmd, ActionListener al, JMenu menu) {
124
        JMenuItem it = new JMenuItem(text, mnemo);
125
        it.setAccelerator(accel);
126
        it.setActionCommand(cmd);
127
        it.addActionListener(al);
128
        menu.add(it);
129
        return it;
130
    }
131
 
132
    protected void updateTitle() {
133
        if (changed) {
134
            setTitle(pgmName + " (modified)");
135
        }
136
        else {
137
            setTitle(pgmName);
138
        }
139
    }
140
 
141
    protected void updateButtons() {
142
        int cnt = table.getSelectedRowCount();
143
        editBut.setEnabled(cnt == 1);
144
        removeBut.setEnabled(cnt > 0);
145
    }
146
 
147
    protected void updateMenus() {
148
        int cnt = table.getSelectedRowCount();
149
        editRowItem.setEnabled(cnt == 1);
150
        removeRowItem.setEnabled(cnt > 0);
151
    }
152
 
153
    protected void setModified(boolean b) {
154
        changed = b;
155
        updateTitle();
156
    }
157
 
158
    protected void load() {
159
        ProcessBuilder pb = new ProcessBuilder("incrontab", "--list");
160
        try {
161
            Process p = pb.start();
162
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
163
            model.importTable(br);
164
            br.close();
165
            boolean end = false;
166
            while (!end) {
167
                try {
168
                    p.waitFor();
169
                    end = true;
170
                } catch (InterruptedException ex) {}
171
            }
172
            setModified(false);
173
        } catch (IOException e) {
174
            JOptionPane.showMessageDialog(this, "Loading incron table failed: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
175
        }
176
    }
177
 
178
    protected void save() {
179
        ProcessBuilder pb = new ProcessBuilder("incrontab", "-");
180
        try {
181
            Process p = pb.start();
182
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
183
            model.exportTable(bw);
184
            bw.close();
185
            boolean end = false;
186
            while (!end) {
187
                try {
188
                    int ev = p.waitFor();
189
                    if (ev != 0)
190
                        throw new IOException("incrontab return value: " + ev);
191
                    end = true;
192
                } catch (InterruptedException ex) {}
193
            }
194
            JOptionPane.showMessageDialog(this, "Table successfully saved.", "Success", JOptionPane.INFORMATION_MESSAGE);
195
            setModified(false);
196
        } catch (IOException e) {
197
            JOptionPane.showMessageDialog(this, "Saving incron table failed: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
198
        }
199
    }
200
 
201
    protected void doExport() {
202
        try {
203
            JFileChooser fc = new JFileChooser();
204
            fc.setDialogTitle("Export incron table");
205
            fc.setDialogType(JFileChooser.SAVE_DIALOG);
206
            fc.setMultiSelectionEnabled(false);
207
            int res = fc.showSaveDialog(this);
208
            if (res == JFileChooser.APPROVE_OPTION) {
209
                File f = fc.getSelectedFile();
210
                if (f.exists()) {
211
                    res = JOptionPane.showConfirmDialog(this, "Selected file already exists. Overwrite?", "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
212
                    if (res != JOptionPane.YES_OPTION)
213
                        return;
214
                }
215
                model.exportToFile(f);
216
                JOptionPane.showMessageDialog(this, "Table successfully exported.", "Success", JOptionPane.INFORMATION_MESSAGE);
217
            }
218
            else if (res == JFileChooser.ERROR_OPTION) {
219
                JOptionPane.showMessageDialog(this, "Error occurred while selecting file for export.", "Error", JOptionPane.ERROR_MESSAGE);
220
            }
221
        } catch (IOException e) {
222
            JOptionPane.showMessageDialog(this, "Cannot export table to file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
223
        }
224
    }
225
 
226
    protected void doImport() {
227
        try {
228
            JFileChooser fc = new JFileChooser();
229
            fc.setDialogTitle("Import incron table");
230
            fc.setDialogType(JFileChooser.OPEN_DIALOG);
231
            fc.setMultiSelectionEnabled(false);
232
            int res = fc.showOpenDialog(this);
233
            if (res == JFileChooser.APPROVE_OPTION) {
234
                File f = fc.getSelectedFile();
235
                model.importFromFile(f);
236
                JOptionPane.showMessageDialog(this, "Table successfully imported.", "Success", JOptionPane.INFORMATION_MESSAGE);
237
                setModified(true);
238
            }
239
            else if (res == JFileChooser.ERROR_OPTION) {
240
                JOptionPane.showMessageDialog(this, "Error occurred while selecting file for import.", "Error", JOptionPane.ERROR_MESSAGE);
241
            }
242
        } catch (IOException e) {
243
            JOptionPane.showMessageDialog(this, "Cannot import table from file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
244
            setModified(true);
245
        }        
246
    }
247
 
248
    protected void addRow() {
249
        ICPropDialog dg = new ICPropDialog(new ICRow("", new InotifyEvent(0, 0), ""));
250
        dg.setVisible(true);
251
        dg.dispose();
252
        if (dg.isCommitted() && dg.isModified()) {
253
            model.addRow(dg.getData());
254
            setModified(true);
255
        }
256
    }
257
 
258
    protected void editRow() {
259
        int ia[] = table.getSelectedRows();
260
        if (ia.length == 1) {
261
            ICPropDialog dg = new ICPropDialog(model.getRow(ia[0]));
262
            dg.setVisible(true);
263
            dg.dispose();
264
            if (dg.isCommitted() && dg.isModified()) {
265
                model.updateRow(ia[0], dg.getData());
266
                setModified(true);
267
            }
268
        }
269
    }
270
 
271
    protected void removeRow() {
272
        int ia[] = table.getSelectedRows();
273
        if (ia.length > 0) {
274
            model.removeRows(ia[0], ia[ia.length-1]);
275
            setModified(true);
276
        }
277
    }
278
 
279
    protected void doDispose() {
280
        ICConfig.setWindowSize(getSize());
281
        ICConfig.setWindowLocation(getLocation());
282
        ICConfig.save();
283
        super.dispose();
284
    }
285
 
286
    public void dispose() {
287
        if (changed) {
288
            int opt = JOptionPane.showConfirmDialog(this,
289
                    "Table data has been modified but not saved.\nDo you want to save it before closing?", "Unsaved data",
290
                    JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
291
            switch (opt) {
292
                case JOptionPane.YES_OPTION:
293
                    save();
294
                case JOptionPane.NO_OPTION:
295
                    doDispose();
296
                default:;
297
            }
298
        }
299
        else {
300
            doDispose();
301
        }
302
    }
303
 
304
    public void actionPerformed(ActionEvent e) {
305
        String s = e.getActionCommand();
306
        if (s.equals("new")) {
307
            model.clear();
308
            setModified(true);
309
        }
310
        else if (s.equals("load")) {
311
            load();
312
        }
313
        else if (s.equals("save")) {
314
            save();
315
        }
316
        else if (s.equals("import")) {
317
            doImport();
318
        }
319
        else if (s.equals("export")) {
320
            doExport();
321
        }
322
        else if (s.equals("quit")) {
323
            dispose();
324
        }
325
        else if (s.equals("add")) {
326
            addRow();
327
        }
328
        else if (s.equals("edit")) {
329
            editRow();
330
        }
331
        else if (s.equals("remove")) {
332
            removeRow();
333
        }
334
        else if (s.equals("about")) {
335
            JOptionPane.showMessageDialog(this, "jIncron 0.1.0", "About jIncron", JOptionPane.INFORMATION_MESSAGE);
336
        }
337
    }
338
 
339
    public void valueChanged(ListSelectionEvent e) {
340
        updateMenus();
341
        updateButtons();
342
    }
343
}