This project has retired. For details please refer to its Attic page.
ActionPanel xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.workbench.swing;
20  
21  import java.awt.BorderLayout;
22  import java.awt.Color;
23  import java.awt.Cursor;
24  import java.awt.Dimension;
25  import java.awt.Font;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  
29  import javax.swing.BorderFactory;
30  import javax.swing.BoxLayout;
31  import javax.swing.JButton;
32  import javax.swing.JComponent;
33  import javax.swing.JFileChooser;
34  import javax.swing.JLabel;
35  import javax.swing.JPanel;
36  import javax.swing.JTextField;
37  import javax.swing.UIManager;
38  
39  import org.apache.chemistry.opencmis.client.api.CmisObject;
40  import org.apache.chemistry.opencmis.workbench.ClientHelper;
41  import org.apache.chemistry.opencmis.workbench.model.ClientModel;
42  
43  public abstract class ActionPanel extends JPanel implements ActionListener {
44  
45      private static final long serialVersionUID = 1L;
46  
47      private final ClientModel model;
48      private CmisObject object;
49  
50      private JPanel centerPanel;
51  
52      public ActionPanel(String title, String buttonLabel, ClientModel model) {
53          super();
54          this.model = model;
55          createGUI(title, buttonLabel);
56      }
57  
58      public ClientModel getClientModel() {
59          return model;
60      }
61  
62      public void setObject(CmisObject object) {
63          this.object = object;
64      }
65  
66      public CmisObject getObject() {
67          return object;
68      }
69  
70      protected void createGUI(String title, String buttonLabel) {
71          BorderLayout borderLayout = new BorderLayout();
72          borderLayout.setVgap(3);
73          setLayout(borderLayout);
74  
75          setBackground(Color.WHITE);
76          setBorder(BorderFactory.createCompoundBorder(
77                  BorderFactory.createEmptyBorder(5, 5, 5, 5),
78                  BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.GRAY, 2),
79                          BorderFactory.createEmptyBorder(5, 5, 5, 5))));
80  
81          Font labelFont = UIManager.getFont("Label.font");
82          Font boldFont = labelFont.deriveFont(Font.BOLD, labelFont.getSize2D() * 1.2f);
83  
84          JLabel titleLabel = new JLabel(title);
85          titleLabel.setFont(boldFont);
86          add(titleLabel, BorderLayout.PAGE_START);
87  
88          centerPanel = new JPanel();
89          centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
90          centerPanel.setBackground(Color.WHITE);
91          add(centerPanel, BorderLayout.CENTER);
92  
93          createActionComponents();
94  
95          JButton deleteButton = new JButton(buttonLabel);
96          deleteButton.addActionListener(this);
97          add(deleteButton, BorderLayout.PAGE_END);
98  
99          setMaximumSize(new Dimension(Short.MAX_VALUE, getPreferredSize().height));
100     }
101 
102     protected void addActionComponent(JComponent comp) {
103         comp.setAlignmentX(LEFT_ALIGNMENT);
104         centerPanel.add(comp);
105     }
106 
107     @Override
108     public void actionPerformed(ActionEvent e) {
109         try {
110             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
111             if (doAction()) {
112                 model.reloadObject();
113             }
114             model.reloadFolder();
115         } catch (Exception ex) {
116             ClientHelper.showError(null, ex);
117         } finally {
118             setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
119         }
120     }
121 
122     protected abstract void createActionComponents();
123 
124     public abstract boolean isAllowed();
125 
126     /**
127      * @return <code>true</code> if object should be reloaded.
128      */
129     public abstract boolean doAction() throws Exception;
130 
131     protected JPanel createFilenamePanel(final JTextField filenameField) {
132         JPanel filePanel = new JPanel(new BorderLayout());
133         filePanel.setBackground(Color.WHITE);
134 
135         filePanel.add(new JLabel("File:"), BorderLayout.LINE_START);
136 
137         filePanel.add(filenameField, BorderLayout.CENTER);
138 
139         JButton browseButton = new JButton("Browse");
140         browseButton.addActionListener(new ActionListener() {
141             @Override
142             public void actionPerformed(ActionEvent event) {
143                 JFileChooser fileChooser = new JFileChooser();
144                 int chooseResult = fileChooser.showDialog(filenameField, "Select");
145                 if (chooseResult == JFileChooser.APPROVE_OPTION) {
146                     if (fileChooser.getSelectedFile().isFile()) {
147                         filenameField.setText(fileChooser.getSelectedFile().getAbsolutePath());
148                     }
149                 }
150             }
151         });
152         filePanel.add(browseButton, BorderLayout.LINE_END);
153 
154         return filePanel;
155     }
156 }