PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/designer/datasource-editor-jdbc/source/org/pentaho/reporting/ui/datasources/jdbc/ui/ConnectionPanel.java

https://gitlab.com/bytekast/pentaho-reporting
Java | 352 lines | 286 code | 41 blank | 25 comment | 22 complexity | a336826cbd1825346d744e5ffb1c4ec0 MD5 | raw file
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it under the
  3. * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
  4. * Foundation.
  5. *
  6. * You should have received a copy of the GNU Lesser General Public License along with this
  7. * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
  8. * or from the Free Software Foundation, Inc.,
  9. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU Lesser General Public License for more details.
  14. *
  15. * Copyright (c) 2009 Pentaho Corporation. All rights reserved.
  16. */
  17. package org.pentaho.reporting.ui.datasources.jdbc.ui;
  18. import java.awt.BorderLayout;
  19. import java.awt.Component;
  20. import java.awt.FlowLayout;
  21. import java.awt.Window;
  22. import java.awt.event.ActionEvent;
  23. import java.beans.PropertyChangeEvent;
  24. import java.beans.PropertyChangeListener;
  25. import java.net.URL;
  26. import java.util.Locale;
  27. import javax.swing.AbstractAction;
  28. import javax.swing.Action;
  29. import javax.swing.Box;
  30. import javax.swing.DefaultComboBoxModel;
  31. import javax.swing.DefaultListCellRenderer;
  32. import javax.swing.ImageIcon;
  33. import javax.swing.JButton;
  34. import javax.swing.JLabel;
  35. import javax.swing.JList;
  36. import javax.swing.JPanel;
  37. import javax.swing.JScrollPane;
  38. import javax.swing.ListSelectionModel;
  39. import javax.swing.event.ListSelectionEvent;
  40. import javax.swing.event.ListSelectionListener;
  41. import org.pentaho.reporting.engine.classic.core.designtime.DesignTimeContext;
  42. import org.pentaho.reporting.libraries.base.util.ObjectUtilities;
  43. import org.pentaho.reporting.libraries.base.util.ResourceBundleSupport;
  44. import org.pentaho.reporting.libraries.base.util.StringUtils;
  45. import org.pentaho.reporting.libraries.designtime.swing.BorderlessButton;
  46. import org.pentaho.reporting.libraries.designtime.swing.LibSwingUtil;
  47. import org.pentaho.reporting.ui.datasources.jdbc.JdbcDataSourceModule;
  48. import org.pentaho.reporting.ui.datasources.jdbc.connection.JdbcConnectionDefinition;
  49. import org.pentaho.ui.xul.XulException;
  50. public abstract class ConnectionPanel extends JPanel
  51. {
  52. private class DataSourceDefinitionListSelectionListener implements ListSelectionListener
  53. {
  54. private JList dataSourceList;
  55. private DataSourceDefinitionListSelectionListener(final JList dataSourceList)
  56. {
  57. this.dataSourceList = dataSourceList;
  58. }
  59. public void valueChanged(final ListSelectionEvent e)
  60. {
  61. getDialogModel().getConnections().setSelectedItem(dataSourceList.getSelectedValue());
  62. }
  63. }
  64. private static class DataSourceDefinitionListCellRenderer extends DefaultListCellRenderer
  65. {
  66. public Component getListCellRendererComponent(final JList list,
  67. final Object value,
  68. final int index,
  69. final boolean isSelected,
  70. final boolean cellHasFocus)
  71. {
  72. final JLabel listCellRendererComponent = (JLabel) super.getListCellRendererComponent(list, value, index,
  73. isSelected,
  74. cellHasFocus);
  75. if (value != null)
  76. {
  77. final String jndiName = ((JdbcConnectionDefinition) value).getName();
  78. if (!"".equals(jndiName))
  79. {
  80. listCellRendererComponent.setText(jndiName);
  81. }
  82. else
  83. {
  84. listCellRendererComponent.setText(" ");
  85. }
  86. }
  87. return listCellRendererComponent;
  88. }
  89. }
  90. private static class SelectionConnectionUpdateHandler implements PropertyChangeListener
  91. {
  92. private JList dataSourceList;
  93. private SelectionConnectionUpdateHandler(final JList dataSourceList)
  94. {
  95. this.dataSourceList = dataSourceList;
  96. }
  97. public void propertyChange(final PropertyChangeEvent aEvent)
  98. {
  99. final DataSourceDialogModel theDialogModel = (DataSourceDialogModel) aEvent.getSource();
  100. final DefaultComboBoxModel theConnections = theDialogModel.getConnections();
  101. final Object theConnection = theConnections.getSelectedItem();
  102. if (theConnection != null)
  103. {
  104. dataSourceList.setSelectedValue(theConnection, true);
  105. }
  106. else
  107. {
  108. dataSourceList.clearSelection();
  109. }
  110. }
  111. }
  112. private class EditDataSourceAction extends AbstractAction implements PropertyChangeListener
  113. {
  114. private JList dataSourceList;
  115. private EditDataSourceAction(final JList dataSourceList)
  116. {
  117. this.dataSourceList = dataSourceList;
  118. final URL location =
  119. ConnectionPanel.class.getResource("/org/pentaho/reporting/ui/datasources/jdbc/resources/Edit.png");
  120. if (location != null)
  121. {
  122. putValue(Action.SMALL_ICON, new ImageIcon(location));
  123. }
  124. else
  125. {
  126. putValue(Action.NAME, bundleSupport.getString("ConnectionPanel.Edit.Name"));
  127. }
  128. putValue(Action.SHORT_DESCRIPTION, bundleSupport.getString("ConnectionPanel.Edit.Description"));
  129. setEnabled(getDialogModel().isConnectionSelected());
  130. }
  131. public void propertyChange(final PropertyChangeEvent evt)
  132. {
  133. setEnabled(getDialogModel().isConnectionSelected());
  134. }
  135. public void actionPerformed(final ActionEvent e)
  136. {
  137. final JdbcConnectionDefinition existingConnection =
  138. (JdbcConnectionDefinition) dataSourceList.getSelectedValue();
  139. final DesignTimeContext designTimeContext = getDesignTimeContext();
  140. try
  141. {
  142. final Window parentWindow = LibSwingUtil.getWindowAncestor(ConnectionPanel.this);
  143. final XulDatabaseDialog connectionDialog = new XulDatabaseDialog(parentWindow, designTimeContext);
  144. final JdbcConnectionDefinition connectionDefinition = connectionDialog.open(existingConnection);
  145. // See if the edit completed...
  146. if (connectionDefinition != null)
  147. {
  148. // If the name changed, delete it before the update is performed
  149. if (existingConnection.getName().equals(connectionDefinition.getName()) == false)
  150. {
  151. getDialogModel().getConnectionDefinitionManager().removeSource(existingConnection.getName());
  152. }
  153. final DataSourceDialogModel dialogModel = getDialogModel();
  154. // Add / update the JNDI source
  155. getDialogModel().getConnectionDefinitionManager().updateSourceList(connectionDefinition);
  156. dialogModel.editConnection(existingConnection, connectionDefinition);
  157. dataSourceList.setSelectedValue(connectionDefinition, true);
  158. }
  159. }
  160. catch (XulException e1)
  161. {
  162. designTimeContext.error(e1);
  163. }
  164. }
  165. }
  166. private class RemoveDataSourceAction extends AbstractAction implements PropertyChangeListener
  167. {
  168. private JList dataSourceList;
  169. /**
  170. * Defines an <code>Action</code> object with a default description string and default icon.
  171. *
  172. * @param dataSourceList the list containing the datasources
  173. */
  174. private RemoveDataSourceAction(final JList dataSourceList)
  175. {
  176. this.dataSourceList = dataSourceList;
  177. setEnabled(getDialogModel().isConnectionSelected());
  178. final URL resource = ConnectionPanel.class.getResource("/org/pentaho/reporting/ui/datasources/jdbc/resources/Remove.png");
  179. if (resource != null)
  180. {
  181. putValue(Action.SMALL_ICON, new ImageIcon(resource));
  182. }
  183. else
  184. {
  185. putValue(Action.NAME, bundleSupport.getString("ConnectionPanel.Remove.Name"));
  186. }
  187. putValue(Action.SHORT_DESCRIPTION, bundleSupport.getString("ConnectionPanel.Remove.Description"));
  188. }
  189. public void propertyChange(final PropertyChangeEvent evt)
  190. {
  191. setEnabled(getDialogModel().isConnectionSelected());
  192. }
  193. public void actionPerformed(final ActionEvent e)
  194. {
  195. final JdbcConnectionDefinition source = (JdbcConnectionDefinition) dataSourceList.getSelectedValue();
  196. if (source != null)
  197. {
  198. getDialogModel().getConnectionDefinitionManager().removeSource(source.getName());
  199. getDialogModel().removeConnection(source);
  200. }
  201. }
  202. }
  203. private class AddDataSourceAction extends AbstractAction
  204. {
  205. private JList dataSourceList;
  206. private AddDataSourceAction(final JList dataSourceList)
  207. {
  208. this.dataSourceList = dataSourceList;
  209. final URL location = ConnectionPanel.class.getResource(
  210. "/org/pentaho/reporting/ui/datasources/jdbc/resources/Add.png");
  211. if (location != null)
  212. {
  213. putValue(Action.SMALL_ICON, new ImageIcon(location));
  214. }
  215. else
  216. {
  217. putValue(Action.NAME, bundleSupport.getString("ConnectionPanel.Add.Name"));
  218. }
  219. putValue(Action.SHORT_DESCRIPTION, bundleSupport.getString("ConnectionPanel.Add.Description"));
  220. }
  221. public void actionPerformed(final ActionEvent e)
  222. {
  223. final DesignTimeContext designTimeContext = getDesignTimeContext();
  224. try
  225. {
  226. final Window parentWindow = LibSwingUtil.getWindowAncestor(ConnectionPanel.this);
  227. final XulDatabaseDialog connectionDialog = new XulDatabaseDialog(parentWindow, designTimeContext);
  228. final JdbcConnectionDefinition connectionDefinition = connectionDialog.open(null);
  229. if (connectionDefinition != null &&
  230. !StringUtils.isEmpty(connectionDefinition.getName()))
  231. {
  232. // A new JNDI source was created
  233. if (getDialogModel().getConnectionDefinitionManager().updateSourceList(connectionDefinition) == false)
  234. {
  235. getDialogModel().addConnection(connectionDefinition);
  236. dataSourceList.setSelectedValue(connectionDefinition, true);
  237. }
  238. }
  239. }
  240. catch (XulException e1)
  241. {
  242. designTimeContext.error(e1);
  243. }
  244. }
  245. }
  246. private DataSourceDialogModel dialogModel;
  247. private DesignTimeContext designTimeContext;
  248. private ResourceBundleSupport bundleSupport;
  249. private boolean securityConfigurationAvailable;
  250. public ConnectionPanel(final DataSourceDialogModel aDialogModel,
  251. final DesignTimeContext designTimeContext)
  252. {
  253. this.securityConfigurationAvailable = true;
  254. this.dialogModel = aDialogModel;
  255. this.designTimeContext = designTimeContext;
  256. this.bundleSupport = new ResourceBundleSupport(Locale.getDefault(), JdbcDataSourceModule.MESSAGES,
  257. ObjectUtilities.getClassLoader(JdbcDataSourceModule.class));
  258. }
  259. protected void initPanel()
  260. {
  261. setLayout(new BorderLayout());
  262. final JList dataSourceList = new JList(dialogModel.getConnections());
  263. dataSourceList.setCellRenderer(new DataSourceDefinitionListCellRenderer());
  264. dataSourceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  265. dataSourceList.addListSelectionListener(new DataSourceDefinitionListSelectionListener(dataSourceList));
  266. dataSourceList.setVisibleRowCount(10);
  267. final SelectionConnectionUpdateHandler theSelectedConnectionAction = new SelectionConnectionUpdateHandler(
  268. dataSourceList);
  269. dialogModel.addPropertyChangeListener(theSelectedConnectionAction);
  270. final EditDataSourceAction editDataSourceAction = new EditDataSourceAction(dataSourceList);
  271. dialogModel.addPropertyChangeListener(editDataSourceAction);
  272. final RemoveDataSourceAction removeDataSourceAction = new RemoveDataSourceAction(dataSourceList);
  273. dialogModel.addPropertyChangeListener(removeDataSourceAction);
  274. final JPanel connectionButtonPanel = new JPanel();
  275. connectionButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  276. if (isSecurityConfigurationAvailable())
  277. {
  278. connectionButtonPanel.add(new JButton(createEditSecurityAction()));
  279. connectionButtonPanel.add(Box.createHorizontalStrut(40));
  280. }
  281. connectionButtonPanel.add(new BorderlessButton(editDataSourceAction));
  282. connectionButtonPanel.add(new BorderlessButton(new AddDataSourceAction(dataSourceList)));
  283. connectionButtonPanel.add(new BorderlessButton(removeDataSourceAction));
  284. final JPanel connectionButtonPanelWrapper = new JPanel(new BorderLayout());
  285. connectionButtonPanelWrapper.add(new JLabel(bundleSupport.getString("ConnectionPanel.Connections")), BorderLayout.CENTER);
  286. connectionButtonPanelWrapper.add(connectionButtonPanel, BorderLayout.EAST);
  287. add(BorderLayout.NORTH, connectionButtonPanelWrapper);
  288. add(BorderLayout.CENTER, new JScrollPane(dataSourceList));
  289. }
  290. protected abstract Action createEditSecurityAction();
  291. public boolean isSecurityConfigurationAvailable()
  292. {
  293. return securityConfigurationAvailable;
  294. }
  295. public void setSecurityConfigurationAvailable(final boolean securityConfigurationAvailable)
  296. {
  297. this.securityConfigurationAvailable = securityConfigurationAvailable;
  298. }
  299. public DataSourceDialogModel getDialogModel()
  300. {
  301. return dialogModel;
  302. }
  303. public DesignTimeContext getDesignTimeContext()
  304. {
  305. return designTimeContext;
  306. }
  307. protected ResourceBundleSupport getBundleSupport()
  308. {
  309. return bundleSupport;
  310. }
  311. }