OSDN Git Service

git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/filelock/trunk@13 c6214a2a-ec3a...
[filelock/repo.git] / filelock / src / main / java / jp / gr / java_conf / u6k / filelock / SwingMain.java
1 /*\r
2  * Copyright (C) 2007 u6k.yu1@gmail.com, All Rights Reserved.\r
3  *\r
4  * Redistribution and use in source and binary forms, with or without\r
5  * modification, are permitted provided that the following conditions\r
6  * are met:\r
7  *\r
8  *    1. Redistributions of source code must retain the above copyright\r
9  *       notice, this list of conditions and the following disclaimer.\r
10  *\r
11  *    2. Redistributions in binary form must reproduce the above copyright\r
12  *       notice, this list of conditions and the following disclaimer in the\r
13  *       documentation and/or other materials provided with the distribution.\r
14  *\r
15  *    3. Neither the name of Clarkware Consulting, Inc. nor the names of its\r
16  *       contributors may be used to endorse or promote products derived\r
17  *       from this software without prior written permission. For written\r
18  *       permission, please contact clarkware@clarkware.com.\r
19  *\r
20  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,\r
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\r
22  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL\r
23  * CLARKWARE CONSULTING OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
26  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
28  * NEGLIGENCE OR OTHERWISE) ARISING IN  ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
30  */\r
31 \r
32 package jp.gr.java_conf.u6k.filelock;\r
33 \r
34 import java.awt.BorderLayout;\r
35 import java.awt.event.ActionEvent;\r
36 import java.awt.event.ActionListener;\r
37 import java.awt.event.KeyEvent;\r
38 import java.awt.event.WindowAdapter;\r
39 import java.awt.event.WindowEvent;\r
40 import java.io.IOException;\r
41 import java.util.ArrayList;\r
42 import java.util.List;\r
43 import java.util.Map;\r
44 import java.util.ResourceBundle;\r
45 import java.util.TreeMap;\r
46 \r
47 import javax.swing.JButton;\r
48 import javax.swing.JFrame;\r
49 import javax.swing.JList;\r
50 import javax.swing.JPanel;\r
51 import javax.swing.UIManager;\r
52 import javax.swing.UnsupportedLookAndFeelException;\r
53 import javax.swing.WindowConstants;\r
54 import javax.swing.border.BevelBorder;\r
55 \r
56 /**\r
57  * <p>\r
58  * Swingアプリケーションとして起動するメイン・クラスです。\r
59  * </p>\r
60  * \r
61  * @author $Author$\r
62  * @version $Rev$ $Date$\r
63  */\r
64 @SuppressWarnings("serial")\r
65 public class SwingMain extends JFrame {\r
66 \r
67     /**\r
68      * <p>\r
69      * ウィンドウの初期幅。\r
70      * </p>\r
71      */\r
72     private static final int       WIDTH  = 400;\r
73 \r
74     /**\r
75      * <p>\r
76      * ウィンドウの初期高さ。\r
77      * </p>\r
78      */\r
79     private static final int       HEIGHT = 300;\r
80 \r
81     /**\r
82      * <p>\r
83      * ロック対象のファイルを表示するリスト。\r
84      * </p>\r
85      */\r
86     private JList                  lockList;\r
87 \r
88     /**\r
89      * <p>\r
90      * 閉じるボタン。\r
91      * </p>\r
92      */\r
93     private JButton                closeButton;\r
94 \r
95     /**\r
96      * <p>\r
97      * ファイル・ロック・ユーティリティ。\r
98      * </p>\r
99      */\r
100     private transient FileLockUtil fileLockUtil;\r
101 \r
102     /**\r
103      * <p>\r
104      * ウィンドウを初期化します。\r
105      * </p>\r
106      * \r
107      * @param args\r
108      *            アプリケーション引数。\r
109      * @throws IOException\r
110      *             ファイルのロックに失敗した場合。\r
111      */\r
112     public SwingMain(String[] args) throws IOException {\r
113         this.fileLockUtil = new FileLockUtil(args);\r
114 \r
115         AppInfo appInfo = new AppInfo();\r
116 \r
117         this.addWindowListener(new WindowAdapter() {\r
118 \r
119             @Override\r
120             public void windowClosed(WindowEvent e) {\r
121                 SwingMain.this.fileLockUtil.release();\r
122                 System.exit(0);\r
123             }\r
124 \r
125         });\r
126 \r
127         this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);\r
128         this.setSize(SwingMain.WIDTH, SwingMain.HEIGHT);\r
129         this.setTitle(appInfo.getTitle() + " " + appInfo.getVersion());\r
130 \r
131         JPanel panel = new JPanel();\r
132         panel.setLayout(new BorderLayout());\r
133         this.getContentPane().add(panel, BorderLayout.CENTER);\r
134 \r
135         this.lockList = new JList();\r
136         this.lockList.setBorder(new BevelBorder(BevelBorder.LOWERED));\r
137         Map<String, Boolean> lockStateMap = new TreeMap<String, Boolean>();\r
138         for (String path : this.fileLockUtil.lockFiles()) {\r
139             lockStateMap.put(path, true);\r
140         }\r
141         for (String path : this.fileLockUtil.lockFailFiles()) {\r
142             lockStateMap.put(path, false);\r
143         }\r
144         List<String> lockListItems = new ArrayList<String>();\r
145         for (Map.Entry<String, Boolean> entry : lockStateMap.entrySet()) {\r
146             if (entry.getValue()) {\r
147                 lockListItems.add("LOCK " + entry.getKey());\r
148             } else {\r
149                 lockListItems.add("FAIL " + entry.getKey());\r
150             }\r
151         }\r
152         this.lockList.setListData(lockListItems.toArray(new String[0]));\r
153         panel.add(this.lockList, BorderLayout.CENTER);\r
154 \r
155         ResourceBundle rb = ResourceBundle.getBundle("filelock");\r
156         this.closeButton = new JButton(rb.getString("message.close"));\r
157         this.closeButton.setMnemonic(KeyEvent.VK_C);\r
158         this.closeButton.addActionListener(new ActionListener() {\r
159 \r
160             public void actionPerformed(ActionEvent e) {\r
161                 SwingMain.this.dispose();\r
162             }\r
163 \r
164         });\r
165         panel.add(this.closeButton, BorderLayout.SOUTH);\r
166     }\r
167 \r
168     /**\r
169      * <p>\r
170      * アプリケーションのエントリーポイントです。\r
171      * </p>\r
172      * \r
173      * @param args\r
174      *            アプリケーション引数。\r
175      * @throws IOException\r
176      *             ファイルのロックに失敗した場合。\r
177      * @throws ClassNotFoundException\r
178      *             レイアウト・マネージャの設定に失敗した場合。\r
179      * @throws InstantiationException\r
180      *             レイアウト・マネージャの設定に失敗した場合。\r
181      * @throws IllegalAccessException\r
182      *             レイアウト・マネージャの設定に失敗した場合。\r
183      * @throws UnsupportedLookAndFeelException\r
184      *             レイアウト・マネージャの設定に失敗した場合。\r
185      */\r
186     public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {\r
187         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());\r
188 \r
189         SwingMain one = new SwingMain(args);\r
190         one.setVisible(true);\r
191     }\r
192 \r
193 }\r