From 46acfc4161ff7c32c4928d62a326eed412996b2d Mon Sep 17 00:00:00 2001
From: Richard Pajersky <xpajersk@fi.muni.cz>
Date: Wed, 3 Feb 2021 16:16:31 +0100
Subject: [PATCH] File chooser opens on preffered path

---
 .../java/cz/fidentis/analyst/gui/Canvas.java  | 24 ++++++-
 .../cz/fidentis/analyst/gui/Installer.java    |  1 +
 .../analyst/gui/PreferencesTopComponent.form  | 14 ++---
 .../analyst/gui/PreferencesTopComponent.java  | 63 +++++++++++++++----
 .../cz/fidentis/analyst/gui/Bundle.properties |  8 +--
 5 files changed, 83 insertions(+), 27 deletions(-)

diff --git a/GUI/src/main/java/cz/fidentis/analyst/gui/Canvas.java b/GUI/src/main/java/cz/fidentis/analyst/gui/Canvas.java
index 433f4045..2c1fae2e 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/gui/Canvas.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/gui/Canvas.java
@@ -12,6 +12,8 @@ import java.awt.Color;
 import java.awt.Cursor;
 import java.awt.Dimension;
 import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.Scanner;
 import java.util.Timer;
 import java.util.TimerTask;
 import javax.swing.ImageIcon;
@@ -151,10 +153,10 @@ public class Canvas extends javax.swing.JPanel {
         
         //enables to shows just .obj files in file chooser
         ModelFileFilter filter = new ModelFileFilter(extensions, "*.obj");
-        JFileChooser jFileChooser1 = new JFileChooser();
+        JFileChooser jFileChooser1 = new JFileChooser(getPrefferedModelPath());
         jFileChooser1.setPreferredSize(new Dimension (800,500));
        
-        jFileChooser1.addChoosableFileFilter(filter);  
+        jFileChooser1.addChoosableFileFilter(filter);        
        
         //file chooser will appear on top of glCanvas
         jFileChooser1.showOpenDialog(this);
@@ -164,6 +166,10 @@ public class Canvas extends javax.swing.JPanel {
         //saves selected file by user as first element in array
         fileArray[0] = jFileChooser1.getSelectedFile();
         
+        //testing getting path TODO
+        JOptionPane.showConfirmDialog(null,
+                fileArray[0].getAbsoluteFile(), "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
+                
         if (fileArray.length <= 0) {
             System.out.print("No file chosen.");
         } else {
@@ -171,6 +177,20 @@ public class Canvas extends javax.swing.JPanel {
         }       
         glCanvas.setVisible(true);
     }
+    
+    private String getPrefferedModelPath() {
+        String data = "";
+        try {
+            File myObj = new File(/*propPath + */"preferences.fip");
+            try (Scanner myReader = new Scanner(myObj)) {
+                data = myReader.nextLine();
+            }            
+        } catch (FileNotFoundException e) {
+            System.out.println("An error occurred.");
+            e.printStackTrace();
+        }
+        return data;
+    }
 
     /**
      * Loads the model in .obj format from file and adds this model to listener for displaying.
diff --git a/GUI/src/main/java/cz/fidentis/analyst/gui/Installer.java b/GUI/src/main/java/cz/fidentis/analyst/gui/Installer.java
index 8d15d50a..0f736386 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/gui/Installer.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/gui/Installer.java
@@ -31,6 +31,7 @@ public class Installer extends ModuleInstall {
             java.util.logging.Logger.getLogger(UserInterface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
         }
         java.awt.EventQueue.invokeLater(() -> {
+            //old JFrame, disabled
             /*frameMain = new UserInterface();
             frameMain.setBackground(new Color(49,165,154));
             frameMain.pack();
diff --git a/GUI/src/main/java/cz/fidentis/analyst/gui/PreferencesTopComponent.form b/GUI/src/main/java/cz/fidentis/analyst/gui/PreferencesTopComponent.form
index 76a96829..45d32458 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/gui/PreferencesTopComponent.form
+++ b/GUI/src/main/java/cz/fidentis/analyst/gui/PreferencesTopComponent.form
@@ -39,10 +39,10 @@
       <Group type="103" groupAlignment="0" attributes="0">
           <Group type="102" alignment="0" attributes="0">
               <EmptySpace max="-2" attributes="0"/>
-              <Component id="jTextField1" min="-2" max="-2" attributes="0"/>
+              <Component id="jFormattedTextField1" pref="288" max="32767" attributes="0"/>
               <EmptySpace max="-2" attributes="0"/>
               <Component id="jButton1" min="-2" max="-2" attributes="0"/>
-              <EmptySpace pref="250" max="32767" attributes="0"/>
+              <EmptySpace min="-2" pref="18" max="-2" attributes="0"/>
           </Group>
       </Group>
     </DimensionLayout>
@@ -51,8 +51,8 @@
           <Group type="102" alignment="0" attributes="0">
               <EmptySpace max="-2" attributes="0"/>
               <Group type="103" groupAlignment="3" attributes="0">
-                  <Component id="jTextField1" alignment="3" min="-2" max="-2" attributes="0"/>
                   <Component id="jButton1" alignment="3" min="-2" max="-2" attributes="0"/>
+                  <Component id="jFormattedTextField1" alignment="3" min="-2" max="-2" attributes="0"/>
               </Group>
               <EmptySpace pref="266" max="32767" attributes="0"/>
           </Group>
@@ -60,17 +60,17 @@
     </DimensionLayout>
   </Layout>
   <SubComponents>
-    <Component class="javax.swing.JTextField" name="jTextField1">
+    <Component class="javax.swing.JButton" name="jButton1">
       <Properties>
         <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
-          <ResourceString bundle="cz/fidentis/analyst/gui/Bundle.properties" key="PreferencesTopComponent.jTextField1.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
+          <ResourceString bundle="cz/fidentis/analyst/gui/Bundle.properties" key="PreferencesTopComponent.jButton1.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
         </Property>
       </Properties>
     </Component>
-    <Component class="javax.swing.JButton" name="jButton1">
+    <Component class="javax.swing.JFormattedTextField" name="jFormattedTextField1">
       <Properties>
         <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
-          <ResourceString bundle="cz/fidentis/analyst/gui/Bundle.properties" key="PreferencesTopComponent.jButton1.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
+          <ResourceString bundle="cz/fidentis/analyst/gui/Bundle.properties" key="PreferencesTopComponent.jFormattedTextField1.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
         </Property>
       </Properties>
     </Component>
diff --git a/GUI/src/main/java/cz/fidentis/analyst/gui/PreferencesTopComponent.java b/GUI/src/main/java/cz/fidentis/analyst/gui/PreferencesTopComponent.java
index 4717de20..8b5e74c0 100644
--- a/GUI/src/main/java/cz/fidentis/analyst/gui/PreferencesTopComponent.java
+++ b/GUI/src/main/java/cz/fidentis/analyst/gui/PreferencesTopComponent.java
@@ -10,9 +10,17 @@ import org.openide.awt.ActionID;
 import org.openide.awt.ActionReference;
 import org.openide.windows.TopComponent;
 import org.openide.util.NbBundle.Messages;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Scanner;
+import javax.swing.JOptionPane;
 
 /**
- * Top component which displays something.
+ *
+ * @author Richard PajerskĂ˝
+ * 
+ * Window with basic Preferences
  */
 @ConvertAsProperties(
         dtd = "-//cz.fidentis.analyst.gui//Preferences//EN",
@@ -25,7 +33,7 @@ import org.openide.util.NbBundle.Messages;
 )
 @TopComponent.Registration(mode = "properties", openAtStartup = false)
 @ActionID(category = "Window", id = "cz.fidentis.analyst.gui.PreferencesTopComponent")
-@ActionReference(path = "Menu/Window" /*, position = 333 */)
+@ActionReference(path = "Menu/Tools", position = 2600, separatorBefore = 2550 /*, position = 333 */)
 @TopComponent.OpenActionRegistration(
         displayName = "#CTL_PreferencesAction",
         preferredID = "PreferencesTopComponent"
@@ -42,7 +50,24 @@ public final class PreferencesTopComponent extends TopComponent {
         setName(Bundle.CTL_PreferencesTopComponent());
         setToolTipText(Bundle.HINT_PreferencesTopComponent());
         putClientProperty(TopComponent.PROP_KEEP_PREFERRED_SIZE_WHEN_SLIDED_IN, Boolean.TRUE);
-
+        
+        createPreferencesFile();
+    }
+    
+    private void createPreferencesFile() {
+        //testing preferences fodler        
+        //String propPath = System.getProperty("user.home") + "/preferences";
+        try {
+            File myObj = new File(/*propPath + */"preferences.fip");
+        if (myObj.createNewFile()) {
+            JOptionPane.showConfirmDialog(null, "File created: " + myObj.getName());
+        } else {
+            JOptionPane.showConfirmDialog(null, "File already exists." + myObj.getName());
+        }
+        } catch (IOException e) {
+        JOptionPane.showConfirmDialog(null, "An error occurred.");
+        e.printStackTrace();
+        }
     }
 
     /**
@@ -53,42 +78,58 @@ public final class PreferencesTopComponent extends TopComponent {
     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
     private void initComponents() {
 
-        jTextField1 = new javax.swing.JTextField();
         jButton1 = new javax.swing.JButton();
-
-        jTextField1.setText(org.openide.util.NbBundle.getMessage(PreferencesTopComponent.class, "PreferencesTopComponent.jTextField1.text")); // NOI18N
+        jFormattedTextField1 = new javax.swing.JFormattedTextField();
 
         org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(PreferencesTopComponent.class, "PreferencesTopComponent.jButton1.text")); // NOI18N
 
+        jFormattedTextField1.setText(org.openide.util.NbBundle.getMessage(PreferencesTopComponent.class, "PreferencesTopComponent.jFormattedTextField1.text")); // NOI18N
+
         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
         this.setLayout(layout);
         layout.setHorizontalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(layout.createSequentialGroup()
                 .addContainerGap()
-                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addComponent(jFormattedTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                 .addComponent(jButton1)
-                .addContainerGap(250, Short.MAX_VALUE))
+                .addGap(18, 18, 18))
         );
         layout.setVerticalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(layout.createSequentialGroup()
                 .addContainerGap()
                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
-                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
-                    .addComponent(jButton1))
+                    .addComponent(jButton1)
+                    .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                 .addContainerGap(266, Short.MAX_VALUE))
         );
     }// </editor-fold>//GEN-END:initComponents
 
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JButton jButton1;
-    private javax.swing.JTextField jTextField1;
+    private javax.swing.JFormattedTextField jFormattedTextField1;
     // End of variables declaration//GEN-END:variables
+    
+    private String getPrefferedModelPath(){
+        String data = "";
+        try {
+            File myObj = new File(/*propPath + */"preferences.fip");
+            try (Scanner myReader = new Scanner(myObj)) {
+                data = myReader.nextLine();
+            }            
+        } catch (FileNotFoundException e) {
+            System.out.println("An error occurred.");
+            e.printStackTrace();
+        }
+        return data;
+    }
+    
     @Override
     public void componentOpened() {
         // TODO add custom code on component opening
+        jFormattedTextField1.setText(getPrefferedModelPath());
     }
 
     @Override
diff --git a/GUI/src/main/resources/cz/fidentis/analyst/gui/Bundle.properties b/GUI/src/main/resources/cz/fidentis/analyst/gui/Bundle.properties
index 5fad024a..87ea39de 100644
--- a/GUI/src/main/resources/cz/fidentis/analyst/gui/Bundle.properties
+++ b/GUI/src/main/resources/cz/fidentis/analyst/gui/Bundle.properties
@@ -1,8 +1,2 @@
-#Localized module labels. Defaults taken from POM (<name>, <description>, <groupId>) if unset.
-#OpenIDE-Module-Name=
-#OpenIDE-Module-Short-Description=
-#OpenIDE-Module-Long-Description=
-#OpenIDE-Module-Display-Category=
-#Thu Oct 03 09:09:53 CEST 2019
-PreferencesTopComponent.jTextField1.text=jTextField1
 PreferencesTopComponent.jButton1.text=jButton1
+PreferencesTopComponent.jFormattedTextField1.text=jFormattedTextField1
-- 
GitLab