OSDN Git Service

パッケージをjp.sourceforge.filelockからjp.gr.java_conf.u6k.filelockに変更しました。
[filelock/repo.git] / filelock / src / main / java / jp / gr / java_conf / u6k / filelock / SwingMain.java
1 /*\r
2  * Copyright (C) 2007 uguu at users.sourceforge.jp, 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 JList                  lockList;\r
73 \r
74     /**\r
75      * <p>\r
76      * 閉じるボタン。\r
77      * </p>\r
78      */\r
79     private JButton                closeButton;\r
80 \r
81     /**\r
82      * <p>\r
83      * ファイル・ロック・ユーティリティ。\r
84      * </p>\r
85      */\r
86     private transient FileLockUtil fileLockUtil;\r
87 \r
88     /**\r
89      * <p>\r
90      * ウィンドウを初期化します。\r
91      * </p>\r
92      * \r
93      * @param args\r
94      *            アプリケーション引数。\r
95      * @throws IOException\r
96      *             ファイルのロックに失敗した場合。\r
97      */\r
98     public SwingMain(String[] args) throws IOException {\r
99         this.fileLockUtil = new FileLockUtil(args);\r
100 \r
101         AppInfo appInfo = new AppInfo();\r
102 \r
103         this.addWindowListener(new WindowAdapter() {\r
104 \r
105             @Override\r
106             public void windowClosed(WindowEvent e) {\r
107                 SwingMain.this.fileLockUtil.release();\r
108                 System.exit(0);\r
109             }\r
110 \r
111         });\r
112 \r
113         this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);\r
114         this.setSize(400, 300);\r
115         this.setTitle(appInfo.getTitle() + " " + appInfo.getVersion());\r
116 \r
117         JPanel panel = new JPanel();\r
118         panel.setLayout(new BorderLayout());\r
119         this.getContentPane().add(panel, BorderLayout.CENTER);\r
120 \r
121         this.lockList = new JList();\r
122         this.lockList.setBorder(new BevelBorder(BevelBorder.LOWERED));\r
123         Map<String, Boolean> lockStateMap = new TreeMap<String, Boolean>();\r
124         for (String path : this.fileLockUtil.lockFiles()) {\r
125             lockStateMap.put(path, true);\r
126         }\r
127         for (String path : this.fileLockUtil.lockFailFiles()) {\r
128             lockStateMap.put(path, false);\r
129         }\r
130         List<String> lockListItems = new ArrayList<String>();\r
131         for (Map.Entry<String, Boolean> entry : lockStateMap.entrySet()) {\r
132             if (entry.getValue()) {\r
133                 lockListItems.add("LOCK " + entry.getKey());\r
134             } else {\r
135                 lockListItems.add("FAIL " + entry.getKey());\r
136             }\r
137         }\r
138         this.lockList.setListData(lockListItems.toArray(new String[0]));\r
139         panel.add(this.lockList, BorderLayout.CENTER);\r
140 \r
141         ResourceBundle rb = ResourceBundle.getBundle("filelock");\r
142         this.closeButton = new JButton(rb.getString("message.close"));\r
143         this.closeButton.setMnemonic(KeyEvent.VK_C);\r
144         this.closeButton.addActionListener(new ActionListener() {\r
145 \r
146             public void actionPerformed(ActionEvent e) {\r
147                 SwingMain.this.dispose();\r
148             }\r
149 \r
150         });\r
151         panel.add(this.closeButton, BorderLayout.SOUTH);\r
152     }\r
153 \r
154     /**\r
155      * <p>\r
156      * アプリケーションのエントリーポイントです。\r
157      * </p>\r
158      * \r
159      * @param args\r
160      *            アプリケーション引数。\r
161      * @throws IOException\r
162      *             ファイルのロックに失敗した場合。\r
163      * @throws ClassNotFoundException\r
164      *             レイアウト・マネージャの設定に失敗した場合。\r
165      * @throws InstantiationException\r
166      *             レイアウト・マネージャの設定に失敗した場合。\r
167      * @throws IllegalAccessException\r
168      *             レイアウト・マネージャの設定に失敗した場合。\r
169      * @throws UnsupportedLookAndFeelException\r
170      *             レイアウト・マネージャの設定に失敗した場合。\r
171      */\r
172     public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {\r
173         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());\r
174 \r
175         SwingMain one = new SwingMain(args);\r
176         one.setVisible(true);\r
177     }\r
178 \r
179 }\r