001/* [{
002Copyright 2008 Nicolas Carranza <nicarran at gmail.com>
003
004This file is part of jpen.
005
006jpen is free software: you can redistribute it and/or modify
007it under the terms of the GNU Lesser General Public License as published by
008the Free Software Foundation, either version 3 of the License,
009or (at your option) any later version.
010
011jpen is distributed in the hope that it will be useful,
012but WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014GNU Lesser General Public License for more details.
015
016You should have received a copy of the GNU Lesser General Public License
017along with jpen.  If not, see <http://www.gnu.org/licenses/>.
018}] */
019package jpen.demo;
020
021import java.awt.BorderLayout;
022import java.awt.Dimension;
023import java.awt.event.ActionEvent;
024import java.awt.event.ActionListener;
025import java.io.IOException;
026import java.util.logging.Logger;
027import javax.swing.BorderFactory;
028import javax.swing.Box;
029import javax.swing.JButton;
030import javax.swing.JComponent;
031import javax.swing.JFrame;
032import javax.swing.JOptionPane;
033import javax.swing.JPanel;
034import javax.swing.JScrollBar;
035import javax.swing.JScrollPane;
036import javax.swing.LookAndFeel;
037import javax.swing.SwingUtilities;
038import javax.swing.UIManager;
039import javax.swing.UnsupportedLookAndFeelException;
040import jpen.demo.inspect.Inspector;
041import jpen.owner.multiAwt.AwtPenToolkit;
042import jpen.PButton;
043import jpen.Pen;
044import jpen.PenManager;
045import jpen.PKind;
046import jpen.PLevel;
047import jpen.PLevelEmulator;
048
049public class JPenDemoControl{
050        private static final Logger L=Logger.getLogger(JPenDemoControl.class.getName());
051        //static{L.setLevel(Level.ALL);}
052
053        //private static final Dimension SIZE=new Dimension(400, 400);
054
055        final MainPanel mainPanel;
056        final JButton statusReportButton=new JButton("Status Report...");
057        final JButton newInstanceButton=new JButton("New Demo Window");
058
059        public JPenDemoControl(){
060                PenManager penManager=AwtPenToolkit.getPenManager();
061                penManager.pen.setFirePenTockOnSwing(true);
062                penManager.pen.setFrequencyLater(40);
063                penManager.pen.levelEmulator.setPressureTriggerForLeftCursorButton(0.5f);
064
065                mainPanel=new MainPanel();
066
067                setSupportCustomPKinds(true);
068
069                statusReportButton.addActionListener(new ActionListener(){
070                                        //@Override
071                                        public void actionPerformed(ActionEvent ev){
072                                                StatusReportPanel statusReportPanel=new StatusReportPanel(
073                                                                        new StatusReport(AwtPenToolkit.getPenManager()));
074                                                //statusReportPanel.panel.setPreferredSize(SIZE);
075                                                JOptionPane.showMessageDialog(mainPanel.panel, statusReportPanel.panel, "JPen Status Report", JOptionPane.INFORMATION_MESSAGE);
076                                        }
077                                });
078                newInstanceButton.addActionListener(new ActionListener(){
079                                        //@Override
080                                        public void actionPerformed(ActionEvent ev){
081                                                JPenDemoControl jpenDemoControl=new JPenDemoControl();
082                                                jpenDemoControl.showFrame();
083                                        }
084                                });
085        }
086
087        public void setSupportCustomPKinds(boolean supportCustomPKinds){
088                mainPanel.devicesPanel.devicesTableModel.
089                setSupportCustomPKinds(supportCustomPKinds);
090        }
091
092        public JComponent getMainPanelComponent(){
093                return mainPanel.panel;
094        }
095
096        public JButton getStatusReportButton(){
097                return statusReportButton;
098        }
099
100        public static void main(String... args) throws IOException, NumberFormatException{
101                setupLookAndFeel();
102                JPenDemoControl jpenDemoControl=new JPenDemoControl();
103                startInspector(AwtPenToolkit.getPenManager());
104                jpenDemoControl.showFrame();
105        }
106
107        static void setupLookAndFeel(){
108                try{
109                        if(System.getProperty("os.name").toLowerCase().contains("linux"))
110                                UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
111                        else
112                                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
113                }catch(Exception ex){
114                        L.warning("The \"system\" look and feel couldn't be set.");
115                }
116        }
117
118        static void startInspector(PenManager penManager) throws IOException{
119                String inspectorPeriodProperty=System.getProperty("jpen.demo.inspectorPeriod");
120                if(inspectorPeriodProperty!=null){
121                        int inspectorPeriod=Integer.valueOf(inspectorPeriodProperty);
122                        Inspector inspector=new Inspector(penManager, "jpen", inspectorPeriod);
123                        L.info("inspector constructed");
124                }
125        }
126
127        void showFrame(){
128                JFrame f=new JFrame("JPen Demo");
129                JPanel framePanel=new JPanel(new BorderLayout(4, 4));
130                framePanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 5, 5));
131                f.setContentPane(framePanel);
132                framePanel.add(mainPanel.panel);
133                Box buttonBox=Box.createHorizontalBox();
134                buttonBox.add(Box.createHorizontalGlue());
135                //buttonBox.add(newInstanceButton);
136                //buttonBox.add(Box.createHorizontalStrut(5));
137                buttonBox.add(statusReportButton);
138                framePanel.add(buttonBox, BorderLayout.SOUTH);
139
140                f.pack();
141                f.setLocationByPlatform(true);
142                f.setVisible(true);
143                f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
144        }
145
146        @Deprecated
147        public void showDialog(JComponent parent, String title){
148                String closeOption="Close";
149                Object[] options=new Object[]{statusReportButton, closeOption};
150                JOptionPane.showOptionDialog(null, getMainPanelComponent(), title,
151                                JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
152                                null, options, closeOption);
153        }
154}