View Javadoc

1   /*
2    * Copyright (C) 2007 u6k.yu1@gmail.com, All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    *
8    *    1. Redistributions of source code must retain the above copyright
9    *       notice, this list of conditions and the following disclaimer.
10   *
11   *    2. Redistributions in binary form must reproduce the above copyright
12   *       notice, this list of conditions and the following disclaimer in the
13   *       documentation and/or other materials provided with the distribution.
14   *
15   *    3. Neither the name of Clarkware Consulting, Inc. nor the names of its
16   *       contributors may be used to endorse or promote products derived
17   *       from this software without prior written permission. For written
18   *       permission, please contact clarkware@clarkware.com.
19   *
20   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
21   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
23   * CLARKWARE CONSULTING OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28   * NEGLIGENCE OR OTHERWISE) ARISING IN  ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package jp.gr.java_conf.u6k.filelock;
33  
34  import java.awt.Container;
35  import java.awt.FontMetrics;
36  import java.awt.Toolkit;
37  import java.awt.event.ActionEvent;
38  import java.awt.event.ActionListener;
39  import java.awt.event.KeyEvent;
40  import java.awt.event.WindowAdapter;
41  import java.awt.event.WindowEvent;
42  import java.io.IOException;
43  import java.util.ArrayList;
44  import java.util.List;
45  import java.util.Map;
46  import java.util.TreeMap;
47  
48  import javax.swing.JButton;
49  import javax.swing.JFrame;
50  import javax.swing.JOptionPane;
51  import javax.swing.JScrollPane;
52  import javax.swing.JTable;
53  import javax.swing.SpringLayout;
54  import javax.swing.UIManager;
55  import javax.swing.UnsupportedLookAndFeelException;
56  import javax.swing.WindowConstants;
57  
58  /**
59   * <p>
60   * Swingアプリケーションとして起動するメイン・クラスです。
61   * </p>
62   * 
63   * @version $Id: SwingMain.java 27 2008-03-05 14:44:06Z u6k $
64   */
65  @SuppressWarnings("serial")
66  class SwingMain extends JFrame {
67  
68      /**
69       * <p>
70       * Swingコンポーネント同士の間隔。
71       * </p>
72       */
73      private static final int COMPONENT_INTERVAL = 10;
74  
75      /**
76       * <p>
77       * ウィンドウの初期幅。
78       * </p>
79       */
80      private static final int WIDTH              = 400;
81  
82      /**
83       * <p>
84       * ウィンドウの初期高さ。
85       * </p>
86       */
87      private static final int HEIGHT             = 300;
88  
89      /**
90       * <p>
91       * ファイルのロック状況を表示するテーブル。
92       * </p>
93       */
94      private JTable           lockTable;
95  
96      /**
97       * <p>
98       * ウィンドウを初期化します。
99       * </p>
100      * 
101      * @param args
102      *            アプリケーション引数。
103      * @throws IOException
104      *             ファイルのロックに失敗した場合。
105      */
106     public SwingMain(String[] args) throws IOException {
107         final FileLockUtil fileLockUtil = new FileLockUtil(args);
108 
109         this.addWindowListener(new WindowAdapter() {
110 
111             @Override
112             public void windowClosed(WindowEvent e) {
113                 fileLockUtil.close();
114                 System.exit(0);
115             }
116 
117         });
118 
119         this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
120         this.setSize(SwingMain.WIDTH, SwingMain.HEIGHT);
121         this.setTitle(ResourceUtil.get("app.title") + " " + ResourceUtil.get("app.version"));
122         this.setIconImage(Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("/jp/gr/java_conf/u6k/filelock/resources/lock.png")));
123         this.setLocationRelativeTo(null);
124 
125         Container c = this.getContentPane();
126         SpringLayout l = new SpringLayout();
127         c.setLayout(l);
128 
129         JButton closeButton = new JButton(ResourceUtil.get("message.close"));
130         closeButton.setMnemonic(KeyEvent.VK_C);
131         closeButton.addActionListener(new ActionListener() {
132 
133             public void actionPerformed(ActionEvent e) {
134                 SwingMain.this.dispose();
135             }
136 
137         });
138         l.putConstraint(SpringLayout.SOUTH, closeButton, -SwingMain.COMPONENT_INTERVAL, SpringLayout.SOUTH, c);
139         l.putConstraint(SpringLayout.EAST, closeButton, -SwingMain.COMPONENT_INTERVAL, SpringLayout.EAST, c);
140         c.add(closeButton);
141 
142         Map<String, Boolean> lockStateMap = new TreeMap<String, Boolean>();
143         for (String path : fileLockUtil.lockFiles()) {
144             lockStateMap.put(path, true);
145         }
146         for (String path : fileLockUtil.lockFailFiles()) {
147             lockStateMap.put(path, false);
148         }
149         List<String[]> lockListItems = new ArrayList<String[]>();
150         for (Map.Entry<String, Boolean> entry : lockStateMap.entrySet()) {
151             if (entry.getValue()) {
152                 lockListItems.add(new String[]{ResourceUtil.get("message.lock"), entry.getKey()});
153             } else {
154                 lockListItems.add(new String[]{ResourceUtil.get("message.fail"), entry.getKey()});
155             }
156         }
157         this.lockTable = new JTable(lockListItems.toArray(new String[0][]), new String[]{"State", "Path"});
158         this.lockTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
159         JScrollPane lockTableScroll = new JScrollPane(this.lockTable);
160         l.putConstraint(SpringLayout.SOUTH, lockTableScroll, -SwingMain.COMPONENT_INTERVAL, SpringLayout.NORTH, closeButton);
161         l.putConstraint(SpringLayout.NORTH, lockTableScroll, SwingMain.COMPONENT_INTERVAL, SpringLayout.NORTH, c);
162         l.putConstraint(SpringLayout.EAST, lockTableScroll, -SwingMain.COMPONENT_INTERVAL, SpringLayout.EAST, c);
163         l.putConstraint(SpringLayout.WEST, lockTableScroll, SwingMain.COMPONENT_INTERVAL, SpringLayout.WEST, c);
164         c.add(lockTableScroll);
165     }
166 
167     private void columnResize() {
168         FontMetrics fm = this.getGraphics().getFontMetrics();
169 
170         for (int columnIndex = 0; columnIndex < this.lockTable.getColumnCount(); columnIndex++) {
171             int maxSize = 0;
172             for (int rowIndex = 0; rowIndex < this.lockTable.getRowCount(); rowIndex++) {
173                 int s = fm.stringWidth((String) this.lockTable.getValueAt(rowIndex, columnIndex));
174                 if (s > maxSize) {
175                     maxSize = s;
176                 }
177             }
178 
179             this.lockTable.getColumnModel().getColumn(columnIndex).setPreferredWidth(maxSize);
180         }
181     }
182 
183     /**
184      * <p>
185      * アプリケーションのエントリーポイントです。
186      * </p>
187      * 
188      * @param args
189      *            アプリケーション引数。
190      * @throws IOException
191      *             ファイルのロックに失敗した場合。
192      * @throws ClassNotFoundException
193      *             レイアウト・マネージャの設定に失敗した場合。
194      * @throws InstantiationException
195      *             レイアウト・マネージャの設定に失敗した場合。
196      * @throws IllegalAccessException
197      *             レイアウト・マネージャの設定に失敗した場合。
198      * @throws UnsupportedLookAndFeelException
199      *             レイアウト・マネージャの設定に失敗した場合。
200      */
201     public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
202         Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
203 
204             public void uncaughtException(Thread t, Throwable e) {
205                 JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
206             }
207 
208         });
209 
210         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
211 
212         SwingMain one = new SwingMain(args);
213         one.setVisible(true);
214         one.columnResize();
215     }
216 
217 }