001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: leinfelder $'
006 * '$Date: 2010-07-29 17:05:50 +0000 (Thu, 29 Jul 2010) $' 
007 * '$Revision: 25179 $'
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
030package org.ecoinformatics.seek.ecogrid;
031
032import java.awt.BorderLayout;
033import java.awt.Color;
034import java.awt.Dimension;
035import java.awt.event.WindowAdapter;
036import java.awt.event.WindowEvent;
037import java.util.Vector;
038
039import javax.swing.JCheckBox;
040import javax.swing.JFrame;
041import javax.swing.JScrollPane;
042import javax.swing.JTable;
043import javax.swing.table.TableColumn;
044
045import org.apache.commons.logging.Log;
046import org.apache.commons.logging.LogFactory;
047import org.kepler.util.StaticResources;
048
049/**
050 * This class will represents a panel which will display services
051 * 
052 * @author Jing Tao
053 * 
054 */
055
056public class ServicesDisplayPanel extends JScrollPane {
057        public static final int LABELPREFERWIDTH = 180;
058        public static final int CELLHEIGHT = 30;
059        public static final int CELLPREFERREDWIDTH = 300;
060        public static final int CELLMINIWIDTH = 200;
061        public static final int CELLMAXWIDTH = 300;
062        public static final String SERVICENAMECOL = 
063                StaticResources.getDisplayString("preferences.data.serviceName", "Service Name");
064        public static final String LOCATIONCOL = 
065                StaticResources.getDisplayString("preferences.data.location", "Location");
066        public static final String DOCUMENTTYPECOL = 
067                StaticResources.getDisplayString("preferences.data.documentType", "Document Type");
068
069        private static final boolean ROWSELECTION = false;
070        private static final boolean CELLSELECTION = false;
071        private static final boolean COLUMNSELECTION = false;
072
073        // private EcoGridServicesController serviceController = null;
074        private Vector selectedServiceList = null;
075        private ServicesDisplayTableModel tableModel = null;
076        private JTable table = null;
077        private CheckBoxTableCellRenderer checkBoxRenderer = null;
078        private TableTableCellRenderer tableRenderer = null;
079        // private JButton responseButton = null;
080
081        // If we need modify columns in table, we need modify this array
082        // and getValueAt method in ServicesDisplayTableModel class
083        public static final String[] HEADNAME = { SERVICENAMECOL, DOCUMENTTYPECOL };
084        private static final int ROWNUMBER = 4;
085
086        protected final static Log log;
087        static {
088                log = LogFactory
089                                .getLog("org.ecoinformatics.seek.ecogrid.ServicesDisplayPanel");
090        }
091
092        /**
093         * Constructor of ServicesDisplayPane
094         * 
095         * @param Vector
096         *            selectedServiceList
097         */
098        public ServicesDisplayPanel(Vector selectedServiceList) {
099                this.selectedServiceList = selectedServiceList;
100                init();
101        }// ServiceDispalyPane
102
103        /*
104         * Method to init the pane
105         */
106        private void init() {
107                this.setPreferredSize(new Dimension(HEADNAME.length
108                                * CELLPREFERREDWIDTH, CELLHEIGHT));
109                this.getViewport().setBackground(Color.WHITE);
110
111                tableModel = new ServicesDisplayTableModel(selectedServiceList,
112                                HEADNAME);
113
114                table = new JTable(tableModel);
115                table.setRowSelectionAllowed(ROWSELECTION);
116                table.setColumnSelectionAllowed(COLUMNSELECTION);
117                table.setCellSelectionEnabled(CELLSELECTION);
118
119                Vector rowHeightFactorList = tableModel.getRowHeightFactor();
120                initRowHeight(rowHeightFactorList);
121                initColumnWidth();
122
123                // first col(service name render use checkbox renderer
124                // second col(location) use default rendered
125                // third col(doctype) use table rendered
126                checkBoxRenderer = new CheckBoxTableCellRenderer(table,
127                                selectedServiceList, CheckBoxTableCellRenderer.DEFAUTTOPROW);
128                tableRenderer = new TableTableCellRenderer(table, selectedServiceList);
129
130                TableColumn serviceNameCol = table.getColumn(SERVICENAMECOL);
131                serviceNameCol.setCellRenderer(checkBoxRenderer);
132                serviceNameCol.setCellEditor(new CheckBoxTableCellEditor(table,
133                                new JCheckBox(), selectedServiceList,
134                                CheckBoxTableCellRenderer.DEFAUTTOPROW));
135
136                TableColumn docTypeCol = table.getColumn(DOCUMENTTYPECOL);
137                docTypeCol.setCellRenderer(tableRenderer);
138                docTypeCol.setCellEditor(new TableTableCellEditor(table,
139                                new JCheckBox(), selectedServiceList));
140
141                this.getViewport().add(table);
142        }// init
143
144        /*
145         * This method picks good column sizes.
146         */
147        private void initColumnWidth() {
148                TableColumn column = null;
149                for (int i = 0; i < tableModel.getColumnCount(); i++) {
150                        column = table.getColumnModel().getColumn(i);
151                        column.setPreferredWidth(CELLPREFERREDWIDTH);
152                        // column.setMaxWidth(CELLMAXWIDTH);
153                        // column.setMinWidth(CELLMINIWIDTH);
154                }// for
155        }// initColumnSizes
156
157        /*
158         * Initial the row height base on given row height factor. If the factor is
159         * null, then table will have unique height
160         */
161        private void initRowHeight(Vector factorList) {
162                if (factorList != null) {
163                        int rowNumber = table.getRowCount();
164                        int listSize = factorList.size();
165                        if (rowNumber <= 0) {
166                                table.setRowHeight(CELLHEIGHT);
167                        }// if
168                        else {
169                                for (int i = 0; i < rowNumber; i++) {
170                                        if (i < listSize) {
171                                                int factor = ((Integer) factorList.elementAt(i))
172                                                                .intValue();
173                                                log.debug("The factor for row " + i + " is " + factor);
174                                                table.setRowHeight(i, factor * CELLHEIGHT);
175                                        } else {
176                                                table.setRowHeight(i, CELLHEIGHT);
177                                        }
178                                }// for
179                        }// else
180                }// if
181                else {
182                        table.setRowHeight(CELLHEIGHT);
183                }//
184        }// initRowHeight
185
186        /**
187         * This method will return a vector which will a whole service was selected
188         * We consider the whole services was seleted: The service name was selected
189         * and all document types in the service were selected.
190         * 
191         * @return Vector
192         */
193        public Vector getAllSelectedServicesList() {
194                Vector list = new Vector();
195                Vector selectedList = tableModel.getSelectedServicesList();
196                if (selectedList != null) {
197                        int length = selectedList.size();
198                        for (int i = 0; i < length; i++) {
199                                SelectableEcoGridService service = (SelectableEcoGridService) selectedList
200                                                .elementAt(i);
201                                SelectableServiceName name = service.getSelectableServiceName();
202                                if (name != null && !name.getIsSelected()) {
203                                        SelectableDocumentType[] types = service
204                                                        .getSelectableDocumentTypeList();
205                                        if (isDocuementTypeAllSelected(types)) {
206                                                list.add(service);
207                                        }// fi
208                                } else if (name != null && name.getIsSelected()) {
209                                        list.add(service);
210                                }
211                        }// for
212                }// if
213                return list;
214        }// getSelectedServicesList
215
216        /**
217         * This method will return a vector which will a whole service was
218         * unselected We consider the whole services was seleted: 1. both service
219         * name and all document types in the service were unselected. 2. service
220         * name is selected, but the all documents types are unselected
221         * 
222         * @return Vector
223         */
224        public Vector getAllUnSelectedServicesList() {
225                Vector list = new Vector();
226                Vector selectedList = tableModel.getSelectedServicesList();
227                if (selectedList != null) {
228                        int length = selectedList.size();
229                        for (int i = 0; i < length; i++) {
230                                SelectableEcoGridService service = (SelectableEcoGridService) selectedList
231                                                .elementAt(i);
232                                SelectableServiceName name = service.getSelectableServiceName();
233                                // if (name != null && !name.getIsSelected())
234                                // {
235                                SelectableDocumentType[] types = service
236                                                .getSelectableDocumentTypeList();
237                                if (isDocuementTypeAllUnSelected(types)) {
238                                        list.add(service);
239                                }// fi
240                                // }//else if
241                        }// for
242                }// if
243                return list;
244        }// getSelectedServicesList
245
246        /*
247         * Method to judge if document types all selected
248         */
249        private boolean isDocuementTypeAllSelected(
250                        SelectableDocumentType[] documentTypes) {
251                boolean allSelected = true;
252                if (documentTypes == null) {
253                        allSelected = false;
254                        return allSelected;
255                }// if
256                else {
257                        int length = documentTypes.length;
258                        for (int i = 0; i < length; i++) {
259                                SelectableDocumentType type = documentTypes[i];
260                                if (type != null && !type.getIsSelected()) {
261                                        allSelected = false;
262                                        break;
263                                }
264                        }//
265                        return allSelected;
266                }// else
267        }// isDocumentTypeAllSelected
268
269        /*
270         * Method to judge if document types all unselected
271         */
272        private boolean isDocuementTypeAllUnSelected(
273                        SelectableDocumentType[] documentTypes) {
274                boolean allUnSelected = true;
275                if (documentTypes == null) {
276                        allUnSelected = false;
277                        return allUnSelected;
278                }// if
279                else {
280                        int length = documentTypes.length;
281                        for (int i = 0; i < length; i++) {
282                                SelectableDocumentType type = documentTypes[i];
283                                if (type != null && type.getIsSelected()) {
284                                        allUnSelected = false;
285                                        break;
286                                }
287                        }//
288                        return allUnSelected;
289                }// else
290        }// isDocumentTypeAllSelected
291
292        /**
293         * Method to return a partial selection serivce list. The service in this
294         * list will only have the selected document type.(We consider the following
295         * case as partial selection: service was selected and only part of document
296         * types was selected)
297         * 
298         * @return Vector
299         */
300        public Vector getPartialSelectedServicesList() {
301                Vector list = getPartilServiceList(true);
302                return list;
303        }// getPartialSelectedServiceList
304
305        /**
306         * This method will return a partial selected service list. The service only
307         * have the unselected document type
308         * 
309         * @return Vector
310         */
311        public Vector getPartialUnselectedServiceList() {
312                Vector list = getPartilServiceList(false);
313                return list;
314        }// getPartialUnSelectedServcieList
315
316        private Vector getPartilServiceList(boolean selected) {
317                Vector list = new Vector();
318                Vector selectedList = tableModel.getSelectedServicesList();
319                if (selectedList != null) {
320                        int length = selectedList.size();
321                        for (int i = 0; i < length; i++) {
322                                SelectableEcoGridService service = (SelectableEcoGridService) selectedList
323                                                .elementAt(i);
324                                SelectableServiceName name = service.getSelectableServiceName();
325                                if (name != null && name.getIsSelected()) {
326                                        SelectableDocumentType[] typeList = service
327                                                        .getSelectableDocumentTypeList();
328                                        Vector newTypeList = new Vector();
329                                        if (typeList != null) {
330                                                int size = typeList.length;
331                                                int count = 0;
332                                                for (int j = 0; j < size; j++) {
333                                                        SelectableDocumentType type = typeList[j];
334                                                        if (selected) {
335                                                                if (type != null && type.getIsSelected()) {
336                                                                        count++;
337                                                                        newTypeList.add(type);
338                                                                } // if
339                                                        } else {
340                                                                if (type != null && !type.getIsSelected()) {
341                                                                        count++;
342                                                                        newTypeList.add(type);
343                                                                }
344                                                        }
345                                                }// for
346                                                if (count > 0 && count < size) {
347                                                        DocumentType[] newTypes = DocumentType
348                                                                        .tranformVectorToArray(newTypeList);
349                                                        service.setDocumentTypeList(newTypes);
350                                                        list.add(service);
351                                                }
352
353                                        }// if
354                                }// if
355                        }// for
356                }// if
357                return list;
358        }
359
360        public static void main(String[] args) {
361                int width = 600;
362                int height = 300;
363                EcoGridServicesController controller = EcoGridServicesController
364                                .getInstance();
365                Vector unSelectedserviceList = controller.getServicesList();
366                // transfer to selectedSerive list(object is SelectedEcoGridService now)
367                Vector selectedServicesList = SelectableEcoGridService
368                                .transferServiceVectToDefaultSelectedServiceVect(unSelectedserviceList);
369
370                ServicesDisplayPanel serviceDisplayPane = new ServicesDisplayPanel(
371                                selectedServicesList);
372                // set up a frame
373                JFrame frame = new JFrame("SwingApplication");
374                frame.setSize(width, height);
375                frame.getContentPane().add(serviceDisplayPane, BorderLayout.CENTER);
376                // Finish setting up the frame, and show it.
377                frame.addWindowListener(new WindowAdapter() {
378                        public void windowClosing(WindowEvent e) {
379                                System.exit(0);
380                        }
381                });
382                frame.setVisible(true);
383
384        }// main
385
386}// ServicesDisplayPane