Search in sources :

Example 1 with File

use of org.opt4j.core.config.annotations.File in project opt4j by felixreimann.

the class PropertyPanel method selectFile.

private void selectFile(Property property) {
    final File file = property.getAnnotation(File.class);
    final JFileChooser fileChooser = PropertyPanel.this.fileChooser.get();
    java.io.File dir = null;
    try {
        dir = new java.io.File(property.getValue().toString());
    } catch (Exception ex) {
    }
    fileChooser.setCurrentDirectory(dir);
    if (file != null && !file.value().equals("")) {
        FileFilter filter = new FileFilter() {

            @Override
            public boolean accept(java.io.File pathname) {
                if (pathname.isDirectory()) {
                    return true;
                }
                String f = pathname.getName().toLowerCase();
                String ext = file.value().toLowerCase();
                if (f.endsWith(ext)) {
                    return true;
                }
                return false;
            }

            @Override
            public String getDescription() {
                String ext = file.value().toLowerCase();
                return "(*" + ext + ")";
            }
        };
        fileChooser.setFileFilter(filter);
    } else {
        fileChooser.setFileFilter(null);
    }
    if (file.folder()) {
        if (file.file()) {
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        } else {
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        }
    } else {
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    }
    fileChooser.setVisible(true);
    int status = fileChooser.showOpenDialog(null);
    if (status == JFileChooser.APPROVE_OPTION) {
        java.io.File f = fileChooser.getSelectedFile();
        try {
            property.setValue(f.getAbsolutePath());
            JTextField field = (JTextField) components.get(property);
            field.setText("" + property.getValue());
            update();
        } catch (InvocationTargetException ex) {
            System.err.println(ex.getMessage());
        }
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) FileFilter(javax.swing.filechooser.FileFilter) JTextField(javax.swing.JTextField) File(org.opt4j.core.config.annotations.File) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with File

use of org.opt4j.core.config.annotations.File in project opt4j by felixreimann.

the class PropertyPanel method updatePropertyPanel.

protected void updatePropertyPanel() {
    panel.removeAll();
    for (final Property property : module.getProperties()) {
        if (property.isActive()) {
            String name = property.getName();
            int i = getIndent(property);
            String s = "";
            for (int j = 0; j < i; j++) {
                s += "     ";
            }
            if (i > 0) {
                s = s.substring(2) + "\u21aa ";
            }
            JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
            JLabel label = new JLabel(s + name);
            label.setFocusable(false);
            String tooltip = format.getTooltip(property);
            if (tooltip != null) {
                label.setToolTipText(tooltip);
            }
            labelPanel.add(label);
            File file = property.getAnnotation(File.class);
            if (file != null) {
                JButton browse = new JButton(Icons.getIcon(Icons.FOLDER));
                browse.setFocusable(false);
                browse.setBorderPainted(false);
                browse.setContentAreaFilled(false);
                browse.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 0));
                browse.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        selectFile(property);
                    }
                });
                browse.setCursor(new Cursor(Cursor.HAND_CURSOR));
                browse.setToolTipText("Browse ...");
                labelPanel.add(browse);
            }
            panel.add(labelPanel);
            Component component = components.get(property);
            panel.add(component);
        }
    }
    if (module.getModule().getClass().isAnnotationPresent(Citation.class)) {
        Citation citation = module.getModule().getClass().getAnnotation(Citation.class);
        addReferenceRow(citation);
    }
}
Also used : JPanel(javax.swing.JPanel) FlowLayout(java.awt.FlowLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) Cursor(java.awt.Cursor) ActionListener(java.awt.event.ActionListener) Citation(org.opt4j.core.config.annotations.Citation) Component(java.awt.Component) Property(org.opt4j.core.config.Property) File(org.opt4j.core.config.annotations.File)

Aggregations

File (org.opt4j.core.config.annotations.File)2 Component (java.awt.Component)1 Cursor (java.awt.Cursor)1 FlowLayout (java.awt.FlowLayout)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URISyntaxException (java.net.URISyntaxException)1 JButton (javax.swing.JButton)1 JFileChooser (javax.swing.JFileChooser)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JTextField (javax.swing.JTextField)1 FileFilter (javax.swing.filechooser.FileFilter)1 Property (org.opt4j.core.config.Property)1 Citation (org.opt4j.core.config.annotations.Citation)1