/* * 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 org.jkan997.slingbeans.nbpropertyeditor; import java.io.IOException; import java.util.Collection; import javax.swing.DefaultComboBoxModel; import javax.swing.table.TableColumn; import org.jkan997.slingbeans.helper.LogHelper; import org.jkan997.slingbeans.helper.PropertyType; import org.jkan997.slingbeans.slingfs.FileObjectAttribute; import org.jkan997.slingbeans.slingfs.SlingFileObject; import org.jkan997.slingbeans.slingfs.local.LocalFileObject; import org.netbeans.api.settings.ConvertAsProperties; import org.openide.awt.ActionID; import org.openide.awt.ActionReference; import org.openide.util.Exceptions; import org.openide.util.Lookup; import org.openide.util.LookupEvent; import org.openide.util.LookupListener; import org.openide.util.NbBundle.Messages; import org.openide.util.Utilities; import org.openide.windows.TopComponent; /** * Top component which displays something. */ @ConvertAsProperties( dtd = "-//org.jkan997.slingbeans.nbpropertyeditor//PropertyEditor//EN", autostore = false ) @TopComponent.Description( preferredID = "PropertyEditorTopComponent", //iconBase="SET/PATH/TO/ICON/HERE", persistenceType = TopComponent.PERSISTENCE_ALWAYS ) @TopComponent.Registration(mode = "output", openAtStartup = true) @ActionID(category = "Window", id = "org.jkan997.slingbeans.nbpropertyeditor.PropertyEditorTopComponent") @ActionReference(path = "Menu/Window" /*, position = 333 */) @TopComponent.OpenActionRegistration( displayName = "Sling Properties", preferredID = "PropertyEditorTopComponent" ) @Messages({ "CTL_PropertyEditorAction=PropertyEditor", "CTL_PropertyEditorTopComponent=PropertyEditor Window", "HINT_PropertyEditorTopComponent=This is a PropertyEditor window" }) public final class PropertyEditorTopComponent extends TopComponent { private final static FileObjectAttribute[] EMPTY_FILE_ATTRS_ARRAY = new FileObjectAttribute[]{}; private final PropertyTableModel propertyTableModel; private Lookup.Result lookupResult; protected SlingFileObject selectedObject; public PropertyEditorTopComponent() { this.setDisplayName("Sling Properties"); propertyTableModel = new PropertyTableModel(); initComponents(); initChangeListener(); TableColumn tc = new TableColumn(0, 200); tc.setHeaderValue("Name"); propertyTable.addColumn(tc); TableColumn valueTc = new TableColumn(1, 400); valueTc.setHeaderValue("Value"); propertyTable.addColumn(valueTc); valueTc.setCellEditor(new PropertyTableCellEditor()); tc = new TableColumn(2, 20); tc.setHeaderValue("Type"); propertyTable.addColumn(tc); tc = new TableColumn(3, 10); tc.setHeaderValue("Read only"); propertyTable.addColumn(tc); propertyTable.setRowHeight(25); propertyTypeCombo.setModel(new DefaultComboBoxModel(PropertyType.TYPES)); propertyTypeCombo.setSelectedIndex(0); propertyTypeComboActionPerformed(null); multiBtn.setVisible(false); } /** * 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() { propertyPane = new javax.swing.JScrollPane(); propertyTable = new javax.swing.JTable(); addPropertyPanel = new javax.swing.JPanel(); propertyNameText = new javax.swing.JTextField(); typeLabel = new javax.swing.JLabel(); valueLabel = new javax.swing.JLabel(); nameLabel = new javax.swing.JLabel(); propertyTypeCombo = new javax.swing.JComboBox(); addButton = new javax.swing.JButton(); multiBtn = new javax.swing.JToggleButton(); valueEditor = new org.jkan997.slingbeans.components.valueeditor.ValueEditorContainer(); propertyTable.setAutoCreateColumnsFromModel(false); propertyTable.setModel(propertyTableModel); propertyPane.setViewportView(propertyTable); addPropertyPanel.setOpaque(false); org.openide.awt.Mnemonics.setLocalizedText(typeLabel, "Type:"); org.openide.awt.Mnemonics.setLocalizedText(valueLabel, "Value:"); org.openide.awt.Mnemonics.setLocalizedText(nameLabel, "Name:"); propertyTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "String" })); propertyTypeCombo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { propertyTypeComboActionPerformed(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(addButton, "+"); addButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addButtonActionPerformed(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(multiBtn, "Multi"); javax.swing.GroupLayout valueEditorLayout = new javax.swing.GroupLayout(valueEditor); valueEditor.setLayout(valueEditorLayout); valueEditorLayout.setHorizontalGroup( valueEditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 231, Short.MAX_VALUE) ); valueEditorLayout.setVerticalGroup( valueEditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE) ); javax.swing.GroupLayout addPropertyPanelLayout = new javax.swing.GroupLayout(addPropertyPanel); addPropertyPanel.setLayout(addPropertyPanelLayout); addPropertyPanelLayout.setHorizontalGroup( addPropertyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(addPropertyPanelLayout.createSequentialGroup() .addContainerGap() .addComponent(nameLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(propertyNameText, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(typeLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(propertyTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(valueLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(valueEditor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(multiBtn) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); addPropertyPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {propertyNameText, propertyTypeCombo}); addPropertyPanelLayout.setVerticalGroup( addPropertyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(addPropertyPanelLayout.createSequentialGroup() .addGroup(addPropertyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(propertyNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(typeLabel) .addComponent(valueLabel) .addComponent(nameLabel) .addComponent(propertyTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(addButton) .addComponent(multiBtn)) .addGap(0, 6, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addPropertyPanelLayout.createSequentialGroup() .addComponent(valueEditor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(propertyPane) .addComponent(addPropertyPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(propertyPane, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(addPropertyPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); }// </editor-fold>//GEN-END:initComponents private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed if (selectedObject == null) { return; } try { String propName = this.propertyNameText.getText(); if ((propName == null) || ("".equals(propName))) { return; } Object propValue = valueEditor.getValue(); int propType = valueEditor.getType(); selectedObject.setAttribute(propName, propValue, propType); selectedObject.saveAttributes(); propertyTableModel.refresh(); } catch (IOException ex) { LogHelper.logError(ex); } }//GEN-LAST:event_addButtonActionPerformed private void propertyTypeComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_propertyTypeComboActionPerformed valueEditor.setTypeAndValue(propertyTypeCombo.getSelectedItem().toString(), null); }//GEN-LAST:event_propertyTypeComboActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addButton; private javax.swing.JPanel addPropertyPanel; private javax.swing.JToggleButton multiBtn; private javax.swing.JLabel nameLabel; private javax.swing.JTextField propertyNameText; private javax.swing.JScrollPane propertyPane; private javax.swing.JTable propertyTable; private javax.swing.JComboBox propertyTypeCombo; private javax.swing.JLabel typeLabel; private org.jkan997.slingbeans.components.valueeditor.ValueEditorContainer valueEditor; private javax.swing.JLabel valueLabel; // End of variables declaration//GEN-END:variables @Override public void componentOpened() { // TODO add custom code on component opening } @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 } public SlingFileObject getSelectedObject() { return selectedObject; } public void setSelectedObject(SlingFileObject selectedObject) { this.selectedObject = selectedObject; boolean localFileObject = (selectedObject instanceof LocalFileObject); addPropertyPanel.setVisible((this.selectedObject != null)&&(!localFileObject)); if (this.selectedObject != null) { Collection<FileObjectAttribute> attrSet = this.selectedObject.getAttributesMap().values(); propertyTableModel.setFileObject(this.selectedObject); } else { propertyTableModel.setFileObject(null); } } private void initChangeListener() { lookupResult = Utilities.actionsGlobalContext().lookupResult(Object.class); lookupResult.addLookupListener(new LookupListener() { @Override public void resultChanged(LookupEvent evt) { Collection<Lookup.Item> allItems = lookupResult.allItems(); String s = ""; SlingFileObject sfo = null; for (Lookup.Item li : allItems) { Object objectInstance = li.getInstance(); LogHelper.logInfo(this, "Selection loop %s", (objectInstance != null ? objectInstance.getClass() : null)); if (objectInstance instanceof SlingFileObject) { sfo = (SlingFileObject) objectInstance; } } if (sfo != null) { LogHelper.logInfo(this, "Selection changed %s", sfo.getFilePath()); setSelectedObject(sfo); } } }); } }