View Javadoc

1   /* OpenLogViewer
2    *
3    * Copyright 2011
4    *
5    * This file is part of the OpenLogViewer project.
6    *
7    * OpenLogViewer software is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * OpenLogViewer software 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 any OpenLogViewer software.  If not, see http://www.gnu.org/licenses/
19   *
20   * I ask that if you make any changes to this file you fork the code on github.com!
21   *
22   */
23  package org.diyefi.openlogviewer.propertypanel;
24  
25  import java.awt.BorderLayout;
26  import java.awt.Color;
27  import java.awt.Dimension;
28  import java.awt.FlowLayout;
29  import java.awt.HeadlessException;
30  import java.awt.event.ActionEvent;
31  import java.awt.event.ActionListener;
32  import java.awt.event.MouseEvent;
33  import java.awt.event.MouseListener;
34  import java.io.BufferedWriter;
35  import java.io.File;
36  import java.io.FileNotFoundException;
37  import java.io.FileReader;
38  import java.io.FileWriter;
39  import java.io.IOException;
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.Scanner;
43  import javax.swing.BorderFactory;
44  import javax.swing.JButton;
45  import javax.swing.JCheckBox;
46  import javax.swing.JColorChooser;
47  import javax.swing.JComboBox;
48  import javax.swing.JFrame;
49  import javax.swing.JLabel;
50  import javax.swing.JMenu;
51  import javax.swing.JMenuBar;
52  import javax.swing.JMenuItem;
53  import javax.swing.JOptionPane;
54  import javax.swing.JPanel;
55  import javax.swing.JScrollPane;
56  import javax.swing.JTextField;
57  import org.diyefi.openlogviewer.OpenLogViewerApp;
58  
59  /**
60   *
61   * @author Bryan Harris
62   */
63  public class PropertiesPane extends JFrame {
64  
65      //ScriptEngineManager mgr = new ScriptEngineManager();
66      //ScriptEngine math = new ScriptEngineManager().getEngineByName("JavaScript");
67      File OLVProperties;
68      ArrayList<SingleProperty> properties;
69      ArrayList<SingleProperty> removeProperties;
70      JPanel propertyPanel;
71      JPanel propertyView;
72  
73      public PropertiesPane(String title) throws HeadlessException {
74          super(title);
75          this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
76          this.setPreferredSize(new Dimension(350, 500));
77          this.setSize(new Dimension(550, 500));
78          this.setJMenuBar(createMenuBar());
79  
80  
81  
82  
83          propertyPanel = new JPanel();
84          propertyPanel.setLayout(new BorderLayout());
85          propertyView = new JPanel();
86          propertyView.setPreferredSize(new Dimension(400, 0));
87          propertyView.setLayout(new FlowLayout(FlowLayout.LEFT));
88  
89          JScrollPane jsp = new JScrollPane(propertyView);
90          propertyPanel.add(jsp, BorderLayout.CENTER);
91          propertyPanel.add(createAcceptPanel(), BorderLayout.SOUTH);
92          this.add(propertyPanel);
93      }
94  
95      public void setProperties(ArrayList<SingleProperty> p) {
96          removeProperties = new ArrayList<SingleProperty>();
97          properties = p;
98          setupForLoad();
99      }
100 
101     private void setupForLoad() {
102         try {
103             String systemDelim = File.separator;
104             File homeDir = new File(System.getProperty("user.home"));
105 
106             if (!homeDir.exists() || !homeDir.canRead() || !homeDir.canWrite()) {
107                 System.out.println("Iether you dont have a home director, or it isnt read/writeable... fix it");
108 
109             } else {
110                 OLVProperties = new File(homeDir.getAbsolutePath() + systemDelim + ".OpenLogViewer");
111             }
112             if (!OLVProperties.exists()) {
113                 try {
114                     if (OLVProperties.mkdir()) {
115                         OLVProperties = new File(homeDir.getAbsolutePath() + systemDelim + ".OpenLogViewer" + systemDelim + "OLVProperties.olv");
116                         if (OLVProperties.createNewFile()) {
117                             loadProperties();
118                         }
119                     } else {
120                         //find somewhere else
121                     }
122                 } catch (IOException IOE) {
123                     System.out.print(IOE.getMessage());
124                 }
125             } else {
126                 OLVProperties = new File(homeDir.getAbsolutePath() + systemDelim + ".OpenLogViewer" + systemDelim + "OLVProperties.olv");
127                 loadProperties();
128             }
129         } catch (Exception E) {
130             System.out.print(E.getMessage());
131         }
132     }
133 
134     private JMenuBar createMenuBar() {
135         JMenuBar propMenuBar = new JMenuBar();
136 
137         JMenu optionMenu = new JMenu("Options");
138         propMenuBar.add(optionMenu);
139         JMenuItem addProp = new JMenuItem("Add New Property");
140         JMenuItem remProp = new JMenuItem("Remove Selected Propertys");
141 
142         addProp.addActionListener(new ActionListener() {
143 
144             @Override
145             public void actionPerformed(ActionEvent evt) {
146                 String s = (String) JOptionPane.showInputDialog(rootPane, "Enter the header for a new property");
147                 if(!s.equals("") || s != null){
148                 SingleProperty newprop = new SingleProperty();
149                 newprop.setHeader(s);
150                 addProperty(newprop);
151                 }
152             }
153         });
154 
155         remProp.addActionListener(new ActionListener() {
156 
157             @Override
158             public void actionPerformed(ActionEvent evt) {
159                 removePropertyPanels();
160             }
161         });
162 
163         optionMenu.add(addProp);
164         optionMenu.add(remProp);
165 
166 
167         return propMenuBar;
168     }
169 
170     private JPanel createAcceptPanel() {
171         JPanel aPanel = new JPanel();
172         aPanel.setPreferredSize(new Dimension(500, 32));
173         aPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 2, 2));
174 
175         JButton okButton = new JButton("OK");
176         JButton cancel = new JButton("Cancel");
177 
178         okButton.addActionListener(new ActionListener() {
179 
180             @Override
181             public void actionPerformed(ActionEvent e) {
182                 OpenLogViewerApp.getInstance().getPropertyPane().save();
183                 OpenLogViewerApp.getInstance().getPropertyPane().setVisible(false);
184             }
185         });
186         cancel.addActionListener(new ActionListener() {
187 
188             @Override
189             public void actionPerformed(ActionEvent e) {
190 
191                 OpenLogViewerApp.getInstance().getPropertyPane().resetProperties();
192                 OpenLogViewerApp.getInstance().getPropertyPane().setVisible(false);
193             }
194         });
195 
196         aPanel.add(cancel);
197         aPanel.add(okButton);
198 
199         return aPanel;
200     }
201 
202     private void loadProperties() {
203         try {
204             Scanner scan = new Scanner(new FileReader(OLVProperties));
205 
206             while (scan.hasNext()) {
207                 String[] propLine = scan.nextLine().split("=");
208                 SingleProperty sp = new SingleProperty();
209                 String[] prop = propLine[1].split(",");
210                 sp.setHeader(propLine[0]);
211                 sp.setColor(new Color(
212                         Integer.parseInt(prop[0]),
213                         Integer.parseInt(prop[1]),
214                         Integer.parseInt(prop[2])));
215                 sp.setMin(Double.parseDouble(prop[3]));
216                 sp.setMax(Double.parseDouble(prop[4]));
217                 sp.setSplit(Integer.parseInt(prop[5]));
218                 sp.setActive(Boolean.parseBoolean(prop[6]));
219                 addProperty(sp);
220             }
221             scan.close();
222 
223         } catch (FileNotFoundException FNF) {
224             System.out.print(FNF.toString());
225         }
226     }
227 
228     public void save() {
229         try {
230             removeProperties.clear();
231             updateProperties();
232             FileWriter fstream = new FileWriter(OLVProperties);
233             BufferedWriter out = new BufferedWriter(fstream);
234             for (int i = 0; i < properties.size(); i++) {
235                 out.write(properties.get(i).toString());
236                 out.newLine();
237             }
238             //Close the output stream
239             out.close();
240         } catch (Exception e) {//Catch exception if any
241             System.err.println("Error: " + e.getMessage());
242         }
243     }
244 
245     private void updateProperties() {
246         for (int i = 0; i < propertyView.getComponentCount(); i++) {
247             PropertyPanel pp = (PropertyPanel) propertyView.getComponent(i);
248             pp.updateSP();
249         }
250     }
251 
252     public void resetProperties() {
253         for (int i = 0; i < propertyView.getComponentCount(); i++) {
254             PropertyPanel pp = (PropertyPanel) propertyView.getComponent(i);
255             pp.reset();
256         }
257         if (removeProperties.size() > 0) {
258             for (int i = 0; i < removeProperties.size(); i++) {
259                 addProperty(removeProperties.get(i));
260             }
261             removeProperties.clear();
262         }
263     }
264 
265     private PropertyPanel exists(SingleProperty sp) {
266 
267         for (int i = 0; i < propertyView.getComponentCount(); i++) {
268             PropertyPanel pp = (PropertyPanel) propertyView.getComponent(i);
269             if (pp.getSp().getHeader().equalsIgnoreCase(sp.getHeader())) {
270                 return pp;
271             }
272         }
273         return null;
274     }
275 
276     public void addProperty(SingleProperty sp) {
277         PropertyPanel pp = exists(sp);
278         if (pp == null) {
279             properties.add(sp);
280             Collections.sort(properties);
281             propertyView.add(new PropertyPanel(sp), properties.indexOf(sp));
282             propertyView.setPreferredSize(new Dimension(propertyView.getPreferredSize().width, propertyView.getPreferredSize().height + 60));
283             propertyView.revalidate();
284         } else {
285             for(int i = 0; i < properties.size(); i++){
286                 if(properties.get(i).getHeader().equalsIgnoreCase(sp.getHeader())){
287                     properties.set(i, sp);
288                 }
289             }
290             pp.setSp(sp);
291             pp.reset();
292         }
293 
294     }
295 
296     public void addPropertyAndSave(SingleProperty sp) {
297         addProperty(sp);
298         save();
299     }
300 
301     private void removeProperty(SingleProperty sp) {
302         if (properties.contains(sp)) {
303             properties.remove(sp);
304         }
305     }
306 
307     private void removePropertyPanels() {
308         for (int i = 0; i < propertyView.getComponentCount();) {
309             PropertyPanel pp = (PropertyPanel) propertyView.getComponent(i);
310             if (pp.getCheck().isSelected()) {
311                 if (!removeProperties.contains(pp.getSp())) {
312                     removeProperties.add(pp.getSp());
313                 }
314                 removeProperty(pp.getSp());/// move this to add to a queue of things to remove, incase of cancel
315                 propertyView.remove(propertyView.getComponent(i));
316                 propertyView.setPreferredSize(new Dimension(propertyView.getPreferredSize().width, propertyView.getPreferredSize().height - 60));
317 
318                 propertyView.revalidate();
319             } else {
320                 i++;
321             }
322         }
323         propertyView.repaint();
324     }
325 
326     private class PropertyPanel extends JPanel implements Comparable {
327 
328         SingleProperty sp;
329         JCheckBox check;
330         JPanel colorBox;
331         JTextField minBox;
332         JTextField maxBox;
333         JTextField splitBox;
334         JComboBox activeBox;
335 
336         public PropertyPanel(SingleProperty sp) {
337             super();
338             this.sp = sp;
339             this.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 2));
340             this.setBorder(BorderFactory.createTitledBorder(sp.getHeader()));
341             setPreferredSize(new Dimension(500, 50));
342             JLabel minLabel = new JLabel("Min:");
343             JLabel maxLabel = new JLabel("Max:");
344             JLabel colorLabel = new JLabel("Color:");
345             JLabel splitLabel = new JLabel("Split:");
346             JLabel activeLabel = new JLabel("Active:");
347             splitBox = new JTextField();
348             splitBox.setPreferredSize(new Dimension(15, 20));
349             splitBox.setText(Integer.toString(sp.getSplit()));
350             minBox = new JTextField();
351             minBox.setPreferredSize(new Dimension(50, 20));
352             minBox.setText(Double.toString(sp.getMin()));
353             maxBox = new JTextField();
354             maxBox.setPreferredSize(new Dimension(50, 20));
355             maxBox.setText(Double.toString(sp.getMax()));
356             colorBox = new JPanel();
357             colorBox.setBackground(sp.getColor());
358             colorBox.setPreferredSize(new Dimension(30, 20));
359             String[] tf = {"False", "True"};
360             activeBox = new JComboBox(tf);
361             if (sp.isActive()) {
362                 activeBox.setSelectedIndex(1);
363             }
364             activeBox.setPreferredSize(new Dimension(60, 20));
365             check = new JCheckBox();
366 
367             colorBox.addMouseListener(new MouseListener() {
368 
369                 @Override
370                 public void mouseReleased(MouseEvent e) {
371                     Color newColor = JColorChooser.showDialog(
372                             OpenLogViewerApp.getInstance().getOptionFrame(),
373                             "Choose New Color", colorBox.getBackground());
374                     if (newColor != null) {
375                         colorBox.setBackground(newColor);
376 
377                     }
378                 }
379 
380                 @Override
381                 public void mouseClicked(MouseEvent e) {
382                 }
383 
384                 @Override
385                 public void mouseEntered(MouseEvent e) {
386                 }
387 
388                 @Override
389                 public void mouseExited(MouseEvent e) {
390                 }
391 
392                 @Override
393                 public void mousePressed(MouseEvent e) {
394                 }
395             });
396             add(colorLabel);
397             add(colorBox);
398             add(minLabel);
399             add(minBox);
400             add(maxLabel);
401             add(maxBox);
402             add(splitLabel);
403             add(splitBox);
404             add(activeLabel);
405             add(activeBox);
406             add(check);
407         }
408 
409         public JCheckBox getCheck() {
410             return check;
411         }
412 
413         public void setCheck(JCheckBox check) {
414             this.check = check;
415         }
416 
417         public SingleProperty getSp() {
418             return sp;
419         }
420 
421         public void setSp(SingleProperty sp) {
422             this.sp = sp;
423         }
424 
425         public void updateSP() {
426             sp.setMin(Double.parseDouble(minBox.getText()));
427             sp.setMax(Double.parseDouble(maxBox.getText()));
428             sp.setColor(colorBox.getBackground());
429             sp.setSplit(Integer.parseInt(splitBox.getText()));
430             String active = (String) activeBox.getSelectedItem();
431             sp.setActive(Boolean.parseBoolean(active));
432         }
433 
434         public void reset() {
435             minBox.setText(Double.toString(sp.getMin()));
436             maxBox.setText(Double.toString(sp.getMax()));
437             colorBox.setBackground(sp.getColor());
438             splitBox.setText(Integer.toString(sp.getSplit()));
439             activeBox.setSelectedItem(Boolean.toString(sp.isActive()));
440         }
441 
442         @Override
443         public int compareTo(Object o) {
444             if (o instanceof PropertyPanel) {
445                 PropertyPanel pp = (PropertyPanel) o;
446                 return this.sp.getHeader().compareToIgnoreCase(pp.getSp().getHeader());
447             } else {
448                 return -1;
449             }
450         }
451     }
452 }