Skip to content
Snippets Groups Projects
PreferencesTopComponent.java 5.86 KiB
Newer Older
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.fidentis.analyst.gui;

import org.netbeans.api.settings.ConvertAsProperties;
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;
 *
 * @author Richard Pajerský
 * 
 * Window with basic Preferences
 */
@ConvertAsProperties(
        dtd = "-//cz.fidentis.analyst.gui//Preferences//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "PreferencesTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE",
        persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "properties", openAtStartup = false)
@ActionID(category = "Window", id = "cz.fidentis.analyst.gui.PreferencesTopComponent")
@ActionReference(path = "Menu/Tools", position = 2600, separatorBefore = 2550 /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_PreferencesAction",
        preferredID = "PreferencesTopComponent"
)
@Messages({
    "CTL_PreferencesAction=Preferences",
    "CTL_PreferencesTopComponent=Preferences Window",
    "HINT_PreferencesTopComponent=This is a Preferences window"
})
public final class PreferencesTopComponent extends TopComponent {

    public PreferencesTopComponent() {
        initComponents();
        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();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        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(jFormattedTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .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(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.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
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}