String unitName = preferences.getLengthUnit().getName();
// Model panel components
this.modelChoiceOrChangeLabel = new JLabel();
this.modelChoiceOrChangeButton = new JButton();
final FurnitureCategory defaultModelCategory =
(importHomePiece || preferences.getFurnitureCatalog().getCategories().size() == 0)
? null
: preferences.getFurnitureCatalog().getCategories().get(0);
this.modelChoiceOrChangeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
String modelName = showModelChoiceDialog(preferences, controller.getContentManager());
if (modelName != null) {
updateController(modelName, preferences,
controller.getContentManager(), defaultModelCategory, false);
}
}
});
this.findModelsButton = new JButton(SwingTools.getLocalizedLabelText(preferences,
ImportedFurnitureWizardStepsPanel.class, "findModelsButton.text"));
BasicService basicService = null;
try {
// Lookup the javax.jnlp.BasicService object
basicService = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService");
// Ignore the basic service, if it doesn't support web browser
if (!basicService.isWebBrowserSupported()) {
basicService = null;
}
} catch (UnavailableServiceException ex) {
// Too bad : service is unavailable
}
final BasicService service = basicService;
this.findModelsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
boolean documentShown = false;
if (service != null) {
try {
// Display Find models page in browser
final URL findModelsUrl = new URL(preferences.getLocalizedString(
ImportedFurnitureWizardStepsPanel.class, "findModelsButton.url"));
documentShown = service.showDocument(findModelsUrl);
} catch (MalformedURLException ex) {
// Document isn't shown
}
}
if (!documentShown) {
// If the document wasn't shown, display a message
// with a copiable URL in a message box
JTextArea findModelsMessageTextArea = new JTextArea(preferences.getLocalizedString(
ImportedFurnitureWizardStepsPanel.class, "findModelsMessage.text"));
String findModelsTitle = preferences.getLocalizedString(
ImportedFurnitureWizardStepsPanel.class, "findModelsMessage.title");
findModelsMessageTextArea.setEditable(false);
findModelsMessageTextArea.setOpaque(false);
JOptionPane.showMessageDialog(SwingUtilities.getRootPane(ImportedFurnitureWizardStepsPanel.this),
findModelsMessageTextArea, findModelsTitle,
JOptionPane.INFORMATION_MESSAGE);
}
}
});
this.modelChoiceErrorLabel = new JLabel(preferences.getLocalizedString(
ImportedFurnitureWizardStepsPanel.class, "modelChoiceErrorLabel.text"));
// Make modelChoiceErrorLabel visible only if an error occurred during model content loading
this.modelChoiceErrorLabel.setVisible(false);
this.modelPreviewComponent = new ModelPreviewComponent();
// Add a transfer handler to model preview component to let user drag and drop a file in component
this.modelPreviewComponent.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(JComponent comp, DataFlavor [] flavors) {
return Arrays.asList(flavors).contains(DataFlavor.javaFileListFlavor);
}
@Override
public boolean importData(JComponent comp, Transferable transferedFiles) {
boolean success = true;
try {
List<File> files = (List<File>)transferedFiles.getTransferData(DataFlavor.javaFileListFlavor);
final String modelName = files.get(0).getAbsolutePath();
EventQueue.invokeLater(new Runnable() {
public void run() {
updateController(modelName, preferences,
controller.getContentManager(), defaultModelCategory, false);
}
});
} catch (UnsupportedFlavorException ex) {
success = false;
} catch (IOException ex) {
success = false;
}
if (!success) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(SwingUtilities.getRootPane(ImportedFurnitureWizardStepsPanel.this),
preferences.getLocalizedString(ImportedFurnitureWizardStepsPanel.class, "modelChoiceError"));
}
});
}
return success;
}
});
this.modelPreviewComponent.setBorder(SwingTools.getDropableComponentBorder());
// Orientation panel components
this.orientationLabel = new JLabel(preferences.getLocalizedString(
ImportedFurnitureWizardStepsPanel.class, "orientationLabel.text"));
this.turnLeftButton = new JButton(new ResourceAction(preferences,
ImportedFurnitureWizardStepsPanel.class, "TURN_LEFT", true) {
@Override
public void actionPerformed(ActionEvent ev) {
Transform3D oldTransform = getModelRotationTransform();
Transform3D leftRotation = new Transform3D();
leftRotation.rotY(-Math.PI / 2);
leftRotation.mul(oldTransform);
updateModelRotation(leftRotation);
}
});
this.turnRightButton = new JButton(new ResourceAction(preferences,
ImportedFurnitureWizardStepsPanel.class, "TURN_RIGHT", true) {
@Override
public void actionPerformed(ActionEvent ev) {
Transform3D oldTransform = getModelRotationTransform();
Transform3D rightRotation = new Transform3D();
rightRotation.rotY(Math.PI / 2);
rightRotation.mul(oldTransform);
updateModelRotation(rightRotation);
}
});
this.turnUpButton = new JButton(new ResourceAction(preferences,
ImportedFurnitureWizardStepsPanel.class, "TURN_UP", true) {
@Override
public void actionPerformed(ActionEvent ev) {
Transform3D oldTransform = getModelRotationTransform();
Transform3D upRotation = new Transform3D();
upRotation.rotX(-Math.PI / 2);
upRotation.mul(oldTransform);
updateModelRotation(upRotation);
}
});
this.turnDownButton = new JButton(new ResourceAction(preferences,
ImportedFurnitureWizardStepsPanel.class, "TURN_DOWN", true) {
@Override
public void actionPerformed(ActionEvent ev) {
Transform3D oldTransform = getModelRotationTransform();
Transform3D downRotation = new Transform3D();
downRotation.rotX(Math.PI / 2);
downRotation.mul(oldTransform);
updateModelRotation(downRotation);
}
});
this.backFaceShownLabel = new JLabel(preferences.getLocalizedString(
ImportedFurnitureWizardStepsPanel.class, "backFaceShownLabel.text"));
this.backFaceShownCheckBox = new JCheckBox(SwingTools.getLocalizedLabelText(preferences,
ImportedFurnitureWizardStepsPanel.class, "backFaceShownCheckBox.text"));
this.backFaceShownCheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
controller.setBackFaceShown(backFaceShownCheckBox.isSelected());
}
});
controller.addPropertyChangeListener(ImportedFurnitureWizardController.Property.BACK_FACE_SHOWN,
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent ev) {
// If back face shown changes update back face shown check box
backFaceShownCheckBox.setSelected(controller.isBackFaceShown());
}
});
this.rotationPreviewComponent = new RotationPreviewComponent(preferences, controller);
// Attributes panel components
this.attributesLabel = new JLabel(preferences.getLocalizedString(
ImportedFurnitureWizardStepsPanel.class, "attributesLabel.text"));
this.nameLabel = new JLabel(SwingTools.getLocalizedLabelText(preferences,
ImportedFurnitureWizardStepsPanel.class, "nameLabel.text"));
this.nameTextField = new JTextField(10);
if (!OperatingSystem.isMacOSXLeopardOrSuperior()) {
SwingTools.addAutoSelectionOnFocusGain(this.nameTextField);
}
final Color defaultNameTextFieldColor = this.nameTextField.getForeground();
DocumentListener nameListener = new DocumentListener() {
public void changedUpdate(DocumentEvent ev) {
nameTextField.getDocument().removeDocumentListener(this);
controller.setName(nameTextField.getText().trim());
nameTextField.getDocument().addDocumentListener(this);
}
public void insertUpdate(DocumentEvent ev) {
changedUpdate(ev);
}
public void removeUpdate(DocumentEvent ev) {
changedUpdate(ev);
}
};
this.nameTextField.getDocument().addDocumentListener(nameListener);
controller.addPropertyChangeListener(ImportedFurnitureWizardController.Property.NAME,
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent ev) {
// If name changes update name text field
if (!nameTextField.getText().trim().equals(controller.getName())) {
nameTextField.setText(controller.getName());
}
updateNameTextFieldForeground(defaultNameTextFieldColor);
}
});
this.addToCatalogCheckBox = new JCheckBox(SwingTools.getLocalizedLabelText(preferences,
ImportedFurnitureWizardStepsPanel.class, "addToCatalogCheckBox.text"));
// Propose the add to catalog option only for home furniture import
this.addToCatalogCheckBox.setVisible(importHomePiece);
this.addToCatalogCheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
if (addToCatalogCheckBox.isSelected()) {
categoryComboBox.setEnabled(true);
controller.setCategory((FurnitureCategory)categoryComboBox.getSelectedItem());
} else {
categoryComboBox.setEnabled(false);
controller.setCategory(null);
}
}
});
this.categoryLabel = new JLabel(SwingTools.getLocalizedLabelText(preferences,
ImportedFurnitureWizardStepsPanel.class, "categoryLabel.text"));
this.categoryComboBox = new JComboBox(preferences.getFurnitureCatalog().getCategories().toArray());
// The piece category isn't enabled by default for home furniture import
this.categoryComboBox.setEnabled(!importHomePiece);
this.categoryComboBox.setEditable(true);
final ComboBoxEditor defaultEditor = this.categoryComboBox.getEditor();
// Change editor to edit category name
this.categoryComboBox.setEditor(new ComboBoxEditor() {
public Object getItem() {
String name = (String)defaultEditor.getItem();
name = name.trim();
// If category is empty, replace it by the last selected item
if (name.length() == 0) {
setItem(categoryComboBox.getSelectedItem());
}
FurnitureCategory category = new FurnitureCategory(name);
// Search an existing category
List<FurnitureCategory> categories = preferences.getFurnitureCatalog().getCategories();
int categoryIndex = Collections.binarySearch(categories, category);
if (categoryIndex >= 0) {
return categories.get(categoryIndex);
}
// If no existing category was found, return a new one
return category;
}
public void setItem(Object value) {
if (value != null) {
FurnitureCategory category = (FurnitureCategory)value;
defaultEditor.setItem(category.getName());
}
}
public void addActionListener(ActionListener l) {
defaultEditor.addActionListener(l);
}
public Component getEditorComponent() {
return defaultEditor.getEditorComponent();
}
public void removeActionListener(ActionListener l) {
defaultEditor.removeActionListener(l);
}
public void selectAll() {
defaultEditor.selectAll();
}
});
this.categoryComboBox.setRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
FurnitureCategory category = (FurnitureCategory)value;
return super.getListCellRendererComponent(list, category.getName(), index, isSelected, cellHasFocus);
}
});
this.categoryComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
controller.setCategory((FurnitureCategory)ev.getItem());
}
});
controller.addPropertyChangeListener(ImportedFurnitureWizardController.Property.CATEGORY,
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent ev) {
// If category changes update category combo box
FurnitureCategory category = controller.getCategory();
if (category != null) {
categoryComboBox.setSelectedItem(category);
}
updateNameTextFieldForeground(defaultNameTextFieldColor);
}