OSDN Git Service

keep JFileChooser status.
authorOlyutorskii <olyutorskii@users.osdn.me>
Sun, 19 Apr 2020 16:11:56 +0000 (01:11 +0900)
committerOlyutorskii <olyutorskii@users.osdn.me>
Sun, 19 Apr 2020 16:11:56 +0000 (01:11 +0900)
src/main/java/jp/sfjp/jindolf/Controller.java
src/main/java/jp/sfjp/jindolf/data/xml/VillageLoader.java

index 5a47ca4..e41dcb8 100644 (file)
@@ -89,6 +89,7 @@ import jp.sfjp.jindolf.view.TopView;
 import jp.sfjp.jindolf.view.WindowManager;
 import jp.sourceforge.jindolf.corelib.VillageState;
 import jp.sourceforge.jovsonz.JsObject;
+import org.xml.sax.SAXException;
 
 /**
  * いわゆるMVCでいうとこのコントローラ。
@@ -110,6 +111,8 @@ public class Controller
 
     private final TopView topView;
 
+    private final JFileChooser xmlFileChooser = buildFileChooser();
+
     private final VillageTreeWatcher treeVillageWatcher =
             new VillageTreeWatcher();
     private final ChangeListener tabPeriodWatcher =
@@ -239,6 +242,24 @@ public class Controller
         return;
     }
 
+    /**
+     * XMLファイルを選択するためのChooserを生成する。
+     *
+     * @return Chooser
+     */
+    private static JFileChooser buildFileChooser(){
+        JFileChooser chooser = new JFileChooser();
+        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
+
+        FileFilter filter;
+        filter = new FileNameExtensionFilter("XML files (*.xml)", "xml", "XML");
+        chooser.setFileFilter(filter);
+
+        chooser.setDialogTitle("アーカイブXMLファイルを開く");
+
+        return chooser;
+    }
+
 
     /**
      * ウィンドウマネジャを返す。
@@ -611,6 +632,8 @@ public class Controller
             return;
         }
 
+        this.xmlFileChooser.updateUI();
+
         LOGGER.log(Level.INFO,
                    "Look&Feelが[{0}]に変更されました。", className );
 
@@ -1172,25 +1195,31 @@ public class Controller
      * ローカルなXMLファイルを読み込む。
      */
     private void actionOpenXml(){
-        JFileChooser chooser = new JFileChooser();
-        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
-
-        FileFilter filter;
-        filter = new FileNameExtensionFilter("XML files (*.xml)", "xml", "XML");
-        chooser.setFileFilter(filter);
-
-        int result = chooser.showOpenDialog(getTopFrame());
+        int result = this.xmlFileChooser.showOpenDialog(getTopFrame());
         if(result != JFileChooser.APPROVE_OPTION) return;
-        File selected = chooser.getSelectedFile();
+        File selected = this.xmlFileChooser.getSelectedFile();
 
         submitHeavyBusyTask(() -> {
             Village village;
+
             try{
                 village = VillageLoader.parseVillage(selected);
             }catch(IOException e){
-                System.out.println(e);
+                String warnMsg = MessageFormat.format(
+                        "XMLファイル[ {0} ]を読み込むことができません",
+                        selected.getPath()
+                );
+                warnDialog("XML I/O error", warnMsg, e);
+                return;
+            }catch(SAXException e){
+                String warnMsg = MessageFormat.format(
+                        "XMLファイル[ {0} ]の形式が不正なため読み込むことができません",
+                        selected.getPath()
+                );
+                warnDialog("XML form error", warnMsg, e);
                 return;
             }
+
             village.setLocalArchive(true);
             AvatarPics avatarPics = village.getAvatarPics();
             this.appSetting.applyLocalImage(avatarPics);
index 43f4ac8..b1d3fdc 100644 (file)
@@ -61,8 +61,10 @@ public class VillageLoader {
      * @param xmlFile XMLファイル
      * @return 村
      * @throws IOException I/Oエラー
+     * @throws SAXException XMLの形式エラー
      */
-    public static Village parseVillage(File xmlFile) throws IOException{
+    public static Village parseVillage(File xmlFile)
+            throws IOException, SAXException{
         Objects.nonNull(xmlFile);
 
         boolean isNormal;
@@ -70,7 +72,7 @@ public class VillageLoader {
                 && xmlFile.exists()
                 && xmlFile.canRead();
         if(!isNormal){
-            return null;
+            throw new IOException(xmlFile.getPath() + "を読み込むことができません");
         }
 
         Path path = xmlFile.toPath();
@@ -85,8 +87,10 @@ public class VillageLoader {
      * @param path XMLファイルのPath
      * @return 村
      * @throws IOException I/Oエラー
+     * @throws SAXException XMLの形式エラー
      */
-    public static Village parseVillage(Path path) throws IOException{
+    public static Village parseVillage(Path path)
+            throws IOException, SAXException{
         Objects.nonNull(path);
 
         path = path.normalize();
@@ -96,7 +100,7 @@ public class VillageLoader {
                 && Files.isRegularFile(path)
                 && Files.isReadable(path);
         if(!isNormal){
-            return null;
+            throw new IOException(path.toString() + "を読み込むことができません");
         }
 
         Village result;
@@ -127,8 +131,10 @@ public class VillageLoader {
      * @param istream XML入力
      * @return 村
      * @throws IOException I/Oエラー
+     * @throws SAXException XMLの形式エラー
      */
-    public static Village parseVillage(InputStream istream) throws IOException{
+    public static Village parseVillage(InputStream istream)
+            throws IOException, SAXException{
         InputSource isource = new InputSource(istream);
         Village result = parseVillage(isource);
         return result;
@@ -140,18 +146,15 @@ public class VillageLoader {
      * @param isource XML入力
      * @return 村
      * @throws IOException I/Oエラー
+     * @throws SAXException XMLの形式エラー
      */
-    public static Village parseVillage(InputSource isource) throws IOException{
+    public static Village parseVillage(InputSource isource)
+            throws IOException, SAXException{
         XMLReader reader = buildReader();
         VillageHandler handler = new VillageHandler();
         reader.setContentHandler(handler);
 
-        try{
-            reader.parse(isource);
-        }catch(SAXException e){
-            System.out.println(e);
-            return null;
-        }
+        reader.parse(isource);
 
         Village result = handler.getVillage();
         return result;