/** Constructor. */ public SaveFileType() { VBoxLayout layout = new VBoxLayout(this, VBoxLayout.Y_AXIS); setLayout(layout); setLayoutType(LayoutType.STATIC); nameLabel.setText("File Name:"); nameLabel.setAlignmentX(0.0f); this.add(nameLabel); input = new VTextField(this, ""); input.setHorizontalAlignment(JTextField.LEFT); setInputDocument(input, input.getDocument()); int height = (int) this.input.getMinimumSize().getHeight(); this.input.setSize(new Dimension(120, height)); this.input.setMinimumSize(new Dimension(120, height)); this.input.setMaximumSize(new Dimension(120, height)); this.input.setPreferredSize(new Dimension(120, height)); input.setAlignmentY(0.5f); input.setAlignmentX(0.0f); this.add(input); final FileDialogManager fileManager = new FileDialogManager(); JButton button = new JButton("..."); button.setMaximumSize(new Dimension(50, button.getMinimumSize().height)); button.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { File directory = null; if (getViewValueWithoutValidation() != null) { directory = new File(getViewValueWithoutValidation().toString()); if (!directory.isDirectory()) { directory = directory.getParentFile(); } } file = fileManager.getSaveFile(getMainCanvas(), directory, getFileFilter(), false); if (file != null) { input.setText(file.toString()); input.setCaretPosition(input.getText().length()); input.setToolTipText(file.toString()); } } }); this.add(button); }
@Override public Object getViewValue() { Object o = null; String inputText = input.getText(); if (inputText.length() > 0) { try { o = new File(inputText); } catch (Exception e) { invalidateValue(); } } return o; }
@Override public void emptyView() { input.setText(""); }
@Override public void setViewValue(Object o) { input.setText(o.toString()); input.setCaretPosition(input.getText().length()); input.setToolTipText(o.toString()); }