Java tutorial
/* * Fresnel Editor */ package cz.muni.fi.fresneleditor.common.reposconf; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.ResourceBundle; import java.util.Set; import javax.swing.DefaultComboBoxModel; import javax.swing.GroupLayout; import javax.swing.JScrollPane; import javax.swing.LayoutStyle; import org.jdesktop.application.Application; import org.openrdf.model.Namespace; import org.openrdf.model.impl.NamespaceImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; import cz.muni.fi.fresneleditor.common.ContextHolder; import cz.muni.fi.fresneleditor.common.FresnelEditorConstants; import cz.muni.fi.fresneleditor.common.ITabComponent; import cz.muni.fi.fresneleditor.common.config.ApplicationConfiguration; import cz.muni.fi.fresneleditor.common.config.RepositoryConfiguration; import cz.muni.fi.fresneleditor.common.guisupport.MessageDialog; import cz.muni.fi.fresneleditor.common.utils.FresnelUtils; import cz.muni.fi.fresneleditor.common.utils.GuiUtils; import cz.muni.fi.fresneleditor.model.BaseRepositoryDao; import cz.muni.fi.fresneleditor.model.DataImportException; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ /** * * @author Igor Zemsky (zemsky@mail.muni.cz) */ public class NamespacesConfigurationJPanel extends javax.swing.JPanel implements ITabComponent<Object> { /** * */ private static final long serialVersionUID = 1L; private static final Logger LOG = LoggerFactory.getLogger(NamespacesConfigurationJPanel.class); private static final ResourceBundle bundle = java.util.ResourceBundle .getBundle(FresnelEditorConstants.PATH_RESOURCE_BUNDLE_COMMON); public static final String NAMESPACES_TAB_LABEL = bundle.getString("Namespaces"); private JScrollPane scrollPane; private List<BaseRepositoryDao> repositoryDaos = new ArrayList<BaseRepositoryDao>(); private Namespace lastSelectedNamespace; /** * Don't use this constructor. */ @Deprecated public NamespacesConfigurationJPanel() { // dummy constructor for netbeans initComponents(); } /** * Creates new form NamespacesConfigurationJPanel */ public NamespacesConfigurationJPanel(List<BaseRepositoryDao> repositoryDaos) { initComponents(); this.repositoryDaos = repositoryDaos; updateRepositoriesCombo(); updateTableRows(); } /** * 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. */ private void initComponents() { namespaceJPanel = new javax.swing.JPanel(); prefixTxtField = new javax.swing.JTextField(); prefixLbl = new javax.swing.JLabel(); nameLbl = new javax.swing.JLabel(); nameTextField = new javax.swing.JTextField(); namespacesScrollPane = new javax.swing.JScrollPane(); namespacesTable = new javax.swing.JTable(); definedNamespacesLbl = new javax.swing.JLabel(); addNewNamespaceBtn = new javax.swing.JButton(); availableRepositoriesLbl = new javax.swing.JLabel(); repositoriesCombo = new javax.swing.JComboBox(); setName("Form"); // NOI18N org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application .getInstance(cz.muni.fi.fresneleditor.common.FresnelApplication.class).getContext() .getResourceMap(NamespacesConfigurationJPanel.class); namespaceJPanel.setBorder(javax.swing.BorderFactory .createTitledBorder(resourceMap.getString("namespaceJPanel.border.title"))); // NOI18N namespaceJPanel.setName("namespaceJPanel"); // NOI18N prefixTxtField.setFont(resourceMap.getFont("prefixTxtField.font")); // NOI18N prefixTxtField.setText(resourceMap.getString("prefixTxtField.text")); // NOI18N prefixTxtField.setName("prefixTxtField"); // NOI18N prefixLbl.setFont(resourceMap.getFont("prefixTxtField.font")); // NOI18N prefixLbl.setText(resourceMap.getString("prefixLbl.text")); // NOI18N prefixLbl.setName("prefixLbl"); // NOI18N nameLbl.setFont(resourceMap.getFont("prefixTxtField.font")); // NOI18N nameLbl.setText(resourceMap.getString("nameLbl.text")); // NOI18N nameLbl.setName("nameLbl"); // NOI18N nameTextField.setFont(resourceMap.getFont("prefixTxtField.font")); // NOI18N nameTextField.setText(resourceMap.getString("nameTextField.text")); // NOI18N nameTextField.setName("nameTextField"); // NOI18N namespacesScrollPane.setName("namespacesScrollPane"); // NOI18N namespacesTable.setModel(new NamespacesTableModel()); namespacesTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS); namespacesTable.setName("namespacesTable"); // NOI18N namespacesTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); namespacesTable.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { namespacesTableMouseClicked(evt); } }); namespacesScrollPane.setViewportView(namespacesTable); namespacesTable.getColumnModel().getColumn(0).setPreferredWidth(100); namespacesTable.getColumnModel().getColumn(0) .setHeaderValue(resourceMap.getString("namespacesTable.columnModel.title0")); // NOI18N namespacesTable.getColumnModel().getColumn(1) .setHeaderValue(resourceMap.getString("namespacesTable.columnModel.title1")); // NOI18N definedNamespacesLbl.setText(resourceMap.getString("definedNamespacesLbl.text")); // NOI18N definedNamespacesLbl.setName("definedNamespacesLbl"); // NOI18N addNewNamespaceBtn.setText(resourceMap.getString("addNewNamespaceBtn.text")); // NOI18N addNewNamespaceBtn.setName("addNewNamespaceBtn"); // NOI18N addNewNamespaceBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addNewNamespaceBtnActionPerformed(evt); } }); javax.swing.GroupLayout namespaceJPanelLayout = new javax.swing.GroupLayout(namespaceJPanel); namespaceJPanel.setLayout(namespaceJPanelLayout); namespaceJPanelLayout.setHorizontalGroup(namespaceJPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(namespaceJPanelLayout.createSequentialGroup().addContainerGap() .addGroup(namespaceJPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(namespacesScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE) .addGroup(namespaceJPanelLayout.createSequentialGroup() .addGroup(namespaceJPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, namespaceJPanelLayout.createSequentialGroup() .addComponent(nameLbl) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(nameTextField)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, namespaceJPanelLayout.createSequentialGroup() .addComponent(prefixLbl) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(prefixTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE) .addComponent(addNewNamespaceBtn)) .addComponent(definedNamespacesLbl)) .addContainerGap())); namespaceJPanelLayout.setVerticalGroup(namespaceJPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(namespaceJPanelLayout.createSequentialGroup().addGap(6, 6, 6) .addComponent(definedNamespacesLbl) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(namespacesScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(namespaceJPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(prefixTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(prefixLbl)) .addGap(9, 9, 9) .addGroup(namespaceJPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(nameLbl) .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(addNewNamespaceBtn)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); availableRepositoriesLbl.setText(resourceMap.getString("availableRepositoriesLbl.text")); // NOI18N availableRepositoriesLbl.setName("availableRepositoriesLbl"); // NOI18N repositoriesCombo.setName("repositoriesCombo"); // NOI18N repositoriesCombo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { repositoriesComboActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setVerticalGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(repositoriesCombo, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(availableRepositoriesLbl, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(namespaceJPanel, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE) .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setHorizontalGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup() .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(availableRepositoriesLbl, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(repositoriesCombo, GroupLayout.PREFERRED_SIZE, 296, GroupLayout.PREFERRED_SIZE) .addGap(0, 77, Short.MAX_VALUE)) .addComponent(namespaceJPanel, GroupLayout.Alignment.LEADING, 0, 586, Short.MAX_VALUE)) .addContainerGap()); Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this); }// </editor-fold>//GEN-END:initComponents private void namespacesTableMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_namespacesTableMouseClicked namespacesTableClicked(); }// GEN-LAST:event_namespacesTableMouseClicked private void repositoriesComboActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_repositoriesComboActionPerformed BaseRepositoryDao repositoryDao = ContextHolder.getInstance() .getRepositoryDao((String) repositoriesCombo.getSelectedItem()); this.repositoryDaos.clear(); this.repositoryDaos.add(repositoryDao); updateTableRows(); }// GEN-LAST:event_repositoriesComboActionPerformed private void addNewNamespaceBtnActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_addNewNamespaceBtnActionPerformed addButtonClicked(); }// GEN-LAST:event_addNewNamespaceBtnActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addNewNamespaceBtn; private javax.swing.JLabel availableRepositoriesLbl; private javax.swing.JLabel definedNamespacesLbl; private javax.swing.JLabel nameLbl; private javax.swing.JTextField nameTextField; private javax.swing.JPanel namespaceJPanel; private javax.swing.JScrollPane namespacesScrollPane; private javax.swing.JTable namespacesTable; private javax.swing.JLabel prefixLbl; private javax.swing.JTextField prefixTxtField; private javax.swing.JComboBox repositoriesCombo; // End of variables declaration//GEN-END:variables /** * Loads the list of available repositories configurations from context * holder and sets this as a model to {@link #repositoriesCombo}. */ private void updateRepositoriesCombo() { ApplicationConfiguration appConf = ContextHolder.getInstance().getApplicationConfiguration(); List<String> v = new ArrayList<String>(); for (String repositoryName : appConf.getRepositoryConfigurations().keySet()) { RepositoryConfiguration repositoryConfiguration = appConf.getRepositoryConfigurations() .get(repositoryName); if (repositoryConfiguration.getLocation() != null && !"".equals(repositoryConfiguration.getLocation())) { v.add(repositoryName); } } Collections.sort(v); repositoriesCombo.setModel(new DefaultComboBoxModel(v.toArray())); if (!v.isEmpty()) { repositoriesCombo.setSelectedIndex(0); } } public void setEnabledIncludingChildren(boolean enabled) { super.setEnabled(enabled); // TODO finish enabling of all component } @Override public Object getItem() { return null; } @Override public String getLabel() { return NAMESPACES_TAB_LABEL; } @Override public JScrollPane getScrollPane() { if (scrollPane == null) { scrollPane = new JScrollPane(this); } return scrollPane; } private void namespacesTableClicked() { // display the clicked value in the fields int selectedRow = namespacesTable.getSelectedRow(); Namespace namespace = null; if (selectedRow != -1) { namespace = ((NamespacesTableModel) namespacesTable.getModel()).getRow(selectedRow); if (!namespace.equals(lastSelectedNamespace)) { // namespace changed prefixTxtField.setText(namespace.getPrefix()); nameTextField.setText(namespace.getName()); lastSelectedNamespace = namespace; } } } private void addButtonClicked() { String prefix = prefixTxtField.getText(); String name = nameTextField.getText(); if (!StringUtils.hasText(prefix)) { String message = "Cannot define namespace with empty prefix."; displayErrorMessage(message); return; } if (!StringUtils.hasText(name)) { String message = "Cannot define namespace with empty name."; displayErrorMessage(message); return; } if (!FresnelUtils.validateNamespacePrefixFormat(prefix)) { String message = "The namespace prefix format is not valid"; displayErrorMessage(message); return; } if (!FresnelUtils.validateNamespaceNameFormat(name)) { String message = "The namespace name format is not valid"; displayErrorMessage(message); return; } String namespaceName = getNamespace(prefix); if (namespaceName != null) { String message = "There already exists namespace with prefix " + prefix + ":<br>" + new NamespaceImpl(prefix, namespaceName).toString(); displayErrorMessage(message); return; } // Miroslav: Namespace will be inserted just to the selected repository Namespace newNamespace = new NamespaceImpl(prefix, name); if (this.repositoryDaos.isEmpty()) { String message = "Cannot add new namespace - error accessing the repository!"; displayErrorMessage(message); LOG.error("Cannot add new namespace - repository DAOs list is empty!"); } else { // Note: Normally there should be only one repository DAO try { for (BaseRepositoryDao dao : this.repositoryDaos) { dao.addNamespace(newNamespace); } } catch (DataImportException ex) { ex.printStackTrace(); LOG.error("Cannot insert new namespace into repository!"); String message = "Cannot add new namespace - error accessing the repository!"; displayErrorMessage(message); } } // insert the namespace into both repositoryDaos // Namespace namespace = new NamespaceImpl(prefix, name); // try { // ContextHolder.getInstance().getFresnelRepositoryDao().addNamespace(namespace); // ContextHolder.getInstance().getDataRepositoryDao().addNamespace(namespace); // } catch (DataImportException e) { // // TODO Auto-generated catch block // // display a message // e.printStackTrace(); // } // refresh the table model // NamespacesTableModel model = (NamespacesTableModel) // namespacesTable.getModel(); // model.setRows(ContextHolder.getInstance().getBothDaosNamespaces()); // model.fireTableDataChanged(); updateTableRows(); } private String getNamespace(String prefix) { String namespace = null; if (repositoryDaos.size() != 0) { namespace = repositoryDaos.get(0).getNamespace(prefix); return namespace; } // namespace = // ContextHolder.getInstance().getFresnelRepositoryDao().getNamespace(prefix); // if (namespace != null) { // return namespace; // } // namespace = // ContextHolder.getInstance().getDataRepositoryDao().getNamespace(prefix); // if (namespace != null) { // return namespace; // } return null; } private void displayErrorMessage(String message) { new MessageDialog(GuiUtils.getOwnerFrame(this), "Cannot add namespace", message).setVisible(true); } private void updateTableRows() { NamespacesTableModel model = (NamespacesTableModel) namespacesTable.getModel(); Set<Namespace> namespaces = new HashSet<Namespace>(); for (BaseRepositoryDao dao : repositoryDaos) { namespaces.addAll(dao.getNamespaces()); } model.setRows(new ArrayList<Namespace>(namespaces)); model.fireTableDataChanged(); } public void setRepositoryDaos(List<BaseRepositoryDao> repositoryDaos) { this.repositoryDaos = repositoryDaos; } public void update(ArrayList<BaseRepositoryDao> repositoryDaos) { setRepositoryDaos(repositoryDaos); updateTableRows(); } }