001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: barseghian $'
006 * '$Date: 2011-02-14 21:34:19 +0000 (Mon, 14 Feb 2011) $' 
007 * '$Revision: 27115 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030/*
031 * 
032 * The Properties Panel is a JPanel that holds the property item inspector.
033 * 
034 */
035
036package org.kepler.reporting.gui;
037
038import java.awt.Dimension;
039import java.awt.GridLayout;
040import java.awt.event.ActionEvent;
041import java.awt.event.WindowAdapter;
042import java.awt.event.WindowEvent;
043
044import javax.swing.DefaultCellEditor;
045import javax.swing.DefaultComboBoxModel;
046import javax.swing.JComboBox;
047import javax.swing.JPanel;
048import javax.swing.JScrollPane;
049import javax.swing.table.AbstractTableModel;
050import javax.swing.table.TableModel;
051
052import org.kepler.gui.TabManager;
053import org.kepler.gui.TabPane;
054import org.kepler.gui.TabPaneActionListener;
055import org.kepler.gui.TabPaneFactory;
056import org.kepler.moml.NamedObjId;
057import org.kepler.objectmanager.lsid.KeplerLSID;
058import org.kepler.reporting.rio.DynamicReportItem;
059import org.kepler.reporting.rio.Item;
060import org.kepler.reporting.rio.StaticReportItemHR;
061import org.kepler.reporting.rio.StaticReportItemImage;
062import org.kepler.reporting.rio.StaticReportItemSection;
063import org.kepler.reporting.rio.StaticReportItemText;
064import org.kepler.reporting.roml.ReportLayout;
065import org.kepler.workflow.WorkflowManager;
066
067import ptolemy.actor.gui.PtolemyFrame;
068import ptolemy.actor.gui.TableauFrame;
069import ptolemy.kernel.util.IllegalActionException;
070import ptolemy.kernel.util.NameDuplicationException;
071import ptolemy.kernel.util.NamedObj;
072
073public class PropertiesPanel extends JPanel implements TabPaneActionListener {
074
075        private TableauFrame _frame;
076        private String _tabName;
077        private Item item;
078        private PropertiesTable table;
079        private TableModel propertiesTableModel;
080        
081        private static final int PROPERTIES_INDEX = 0;
082        private static final int HIDDEN_INDEX = 2;
083
084        public PropertiesPanel() {
085
086                super(new GridLayout(1, 0));
087        }
088
089        public void actionPerformed(ActionEvent e) {
090
091                Object src = e.getSource();
092                
093                if (src instanceof ReportDesignerPanel) {
094
095                        ReportDesignerPanel rdp = (ReportDesignerPanel) src;
096                        this.item = rdp.getSelectedItem(); // knows what item it has here.
097
098                        for (int i = 0; i < table.getRowCount(); i++) {
099
100                                DefaultComboBoxModel model = getEditorModel(this.item, i);
101                                RowEditorModel rm = new RowEditorModel();
102                                table.setRowEditorModel(rm);
103                                JComboBox tmpBox = new JComboBox();
104                                tmpBox.setModel(model);
105                                tmpBox.setEditable(true);
106                                DefaultCellEditor tmpEditor = new DefaultCellEditor(tmpBox);
107                                table.rm.addEditorForRow(i, tmpEditor);
108                        }
109                }
110
111                for (int i = 0; i < table.getRowCount(); i++) {
112                        JComboBox tmpBox = new JComboBox();
113                        tmpBox.setEditable(true);
114                        tmpBox.setModel(getEditorModel(this.item, i));
115                        DefaultCellEditor tmpEditor = new DefaultCellEditor(tmpBox);
116                        table.rm.addEditorForRow(i, tmpEditor);
117                }
118
119                table.setColumnSelectionAllowed(false);
120                table.setRowSelectionAllowed(false);
121
122                ((AbstractTableModel) propertiesTableModel).fireTableDataChanged();
123                ((AbstractTableModel) propertiesTableModel).fireTableRowsUpdated(
124                                PROPERTIES_INDEX, HIDDEN_INDEX);
125
126        }
127
128
129        public JPanel createPropertiesPanel() {
130
131                JPanel propertiesPanel = new JPanel();
132
133                propertiesTableModel = new AbstractTableModel() {
134
135                        String[] columnNames = { "Property", "Value" };
136
137                        public String getColumnName(int col) {
138                                return columnNames[col].toString();
139                        }
140
141                        public int getColumnCount() {
142                                return columnNames.length;
143                        }
144
145                        public boolean isCellEditable(int row, int col) {
146
147                                if (col == HIDDEN_INDEX) {
148                                        return false;
149                                } else if (col == PROPERTIES_INDEX) {
150                                        return false;
151                                } else {
152                                        return true;
153                                }
154
155                        }
156
157                        public int getRowCount() {
158                                if (item != null) {
159                                        return item.getItemProperties().getProperties().size();
160                                }
161                                return 0;
162                        }
163
164                        public Object getValueAt(int row, int col) {
165
166                                if (item != null) {
167                                        String key = item.getItemProperties()
168                                                        .getPropertyNameAt(row);
169                                        if (col == 0) {
170                                                return key;
171                                        } else {
172                                                return item.getItemProperties().getProperty(key);
173                                        }
174                                }
175                                return null;
176                        }
177
178                        public void setValueAt(Object value, int row, int col) {
179
180                                if (item != null) {
181                                        
182                                        // fix for http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5301
183                                        // XXX a better fix would be if Properties table cell editors
184                                        // lost focus when changing report items
185                                        if (row >= item.getItemProperties().getProperties().size()){
186                                                return;
187                                        }
188                                        // end fix
189                                        
190                                        String key = item.getItemProperties()
191                                                        .getPropertyNameAt(row);
192                                        value = value.toString();
193                                        boolean markTableauFrameModified = 
194                                                item.getItemProperties().setProperty(key, (String) value);
195                                        if (markTableauFrameModified){
196                                                
197                                                //look up the reportLayout
198                                                NamedObj reference = ((PtolemyFrame) _frame).getModel();
199                                                KeplerLSID lsid = NamedObjId.getIdFor(reference);
200                                                ReportLayout reportLayout = WorkflowManager.getInstance().getWorkflow(_frame, lsid)
201                                                                .getReportLayout(_frame);
202                                                                
203                                                reportLayout.setModified(_frame);
204                                        }
205                                        fireTableCellUpdated(row, col); // added this
206                                }
207                        }
208
209                };
210
211                // initialize the Table
212
213                //we have to get the table to read in values (maybe indexes?) from the ROML.
214                table = new PropertiesTable((AbstractTableModel) propertiesTableModel); 
215
216                JScrollPane scrollpane = new JScrollPane(table,
217                                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
218                                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
219
220                scrollpane.setPreferredSize(new Dimension(200, 100));
221                scrollpane.setMaximumSize(new Dimension (200, 200));
222                scrollpane.setMinimumSize(new Dimension(200, 100));
223
224                propertiesPanel.add(scrollpane);
225
226                return propertiesPanel;
227        }
228
229        private DefaultComboBoxModel getEditorModel(Item item, int row) {
230
231                DefaultComboBoxModel model = new DefaultComboBoxModel();
232
233                if (item instanceof StaticReportItemImage) {
234
235                        switch (row) {
236                        case StaticReportItemImage.ROW_SCALE:
237                                model = new DefaultComboBoxModel(
238                                                StaticReportItemImage.SCALE_VALUES.values());
239                                break;
240                        }
241
242                } else if (item instanceof StaticReportItemHR) {
243
244                        switch (row) {
245                        case StaticReportItemHR.ROW_ALIGNMENT:
246                                model = new DefaultComboBoxModel(
247                                                StaticReportItemHR.ALIGNMENT_VALUES.values());
248                                break;
249                        case StaticReportItemHR.ROW_WIDTH:
250                                model = new DefaultComboBoxModel(
251                                                StaticReportItemHR.WIDTH_VALUES.values());
252                                break;
253                        }
254
255                } else if (item instanceof StaticReportItemSection) {
256                        switch (row) {
257                        case StaticReportItemSection.ROW_ALIGNMENT:
258                                model = new DefaultComboBoxModel(
259                                                StaticReportItemSection.AlignmentValues.values());
260                                break;
261                        case StaticReportItemSection.ROW_BREAK_BEFORE:
262                                model = new DefaultComboBoxModel(
263                                                StaticReportItemSection.BreakValues.values());
264                                break;
265                        case StaticReportItemSection.ROW_SIZE:
266                                model = new DefaultComboBoxModel(
267                                                StaticReportItemSection.FontSizeValues.values());
268                                break;
269                        }
270
271                } else if (item instanceof StaticReportItemText) {
272                        switch (row) {
273                        case StaticReportItemText.ROW_ALIGNMENT:
274                                model = new DefaultComboBoxModel(
275                                                StaticReportItemText.TextAlignment.values());
276                                break;
277                        case StaticReportItemText.ROW_SIZE:
278                                model = new DefaultComboBoxModel(StaticReportItemText.TextSize
279                                                .values());
280                                break;
281                        case StaticReportItemText.ROW_WEIGHT:
282                                model = new DefaultComboBoxModel(
283                                                StaticReportItemText.TextWeight.values());
284                                break;
285                        }
286
287                } else if (item instanceof DynamicReportItem) {
288                        switch (row) {
289                        case DynamicReportItem.ROW_ALIGNMENT:
290                                model = new DefaultComboBoxModel(
291                                                DynamicReportItem.AlignmentValues.values());
292                                break;
293                        case DynamicReportItem.ROW_SCALE:
294                                model = new DefaultComboBoxModel(DynamicReportItem.ScaleValues
295                                                .values());
296                                break;
297                        }
298                }
299
300                return model;
301        }
302
303        /**
304         * Implementation of TabPane getName()
305         */
306        public String getTabName() {
307                return "Properties";
308        }
309
310        public void setTabName(String name) {
311                _tabName = name;
312        }
313
314        /**
315         * Implementation of TabPane setParentFrame(TableauFrame)
316         */
317        public void setParentFrame(TableauFrame parent) {
318                _frame = parent;
319        }
320
321        /**
322         * Implementation of getParentFrame getName()
323         */
324        public TableauFrame getParentFrame() {
325                return _frame;
326        }
327
328        public void initializeTab() throws Exception {
329
330                TabManager.getInstance()
331                                .addTabPaneListener(this.getParentFrame(), this);
332
333                JPanel properties = createPropertiesPanel();
334
335                add(properties);
336                
337                // clean up the listener when the frame closes
338                   final TabPaneActionListener listenerReference = this;
339                    _frame.addWindowListener(new WindowAdapter() {
340                                public void windowClosed(WindowEvent e) {
341                                        TabManager.getInstance().removeTabPaneListener(listenerReference);
342                                }
343                        });
344
345        }
346
347        public static class Factory extends TabPaneFactory {
348                /**
349                 * Create a factory with the given name and container.
350                 * 
351                 *@param container
352                 *            The container.
353                 *@param name
354                 *            The name of the entity.
355                 *@exception IllegalActionException
356                 *                If the container is incompatible with this attribute.
357                 *@exception NameDuplicationException
358                 *                If the name coincides with an attribute already in the
359                 *                container.
360                 */
361                public Factory(NamedObj container, String name)
362                                throws IllegalActionException, NameDuplicationException {
363                        super(container, name);
364                }
365
366                /**
367                 * Create a library pane that displays the given library of actors.
368                 * 
369                 * @return A new LibraryPaneTab that displays the library
370                 */
371                public TabPane createTabPane(TableauFrame parent) {
372                        PropertiesPanel pp = new PropertiesPanel();
373                        pp.setTabName(this.getName());
374                        return pp;
375                }
376        }
377
378}