private JTable initTable()

in taverna-credential-manager-ui/src/main/java/org/apache/taverna/workbench/ui/credentialmanager/CredentialManagerUI.java [309:652]


	private JTable initTable(String tableType, JPanel tab) {
		JTable table = null;

		if (tableType.equals(PASSWORDS)) { // Passwords table
			// The Passwords table's data model
			PasswordsTableModel passwordsTableModel = new PasswordsTableModel(credManager);
			// The table itself
			table = new JTable(passwordsTableModel);

			/*
			 * Set the password and alias columns of the Passwords table to be
			 * invisible by removing them from the column model (they will still
			 * present in the table model)
			 * 
			 * Remove the last column first
			 */
			TableColumn aliasColumn = table.getColumnModel().getColumn(5);
			table.getColumnModel().removeColumn(aliasColumn);
			TableColumn passwordColumn = table.getColumnModel().getColumn(4);
			table.getColumnModel().removeColumn(passwordColumn);
			TableColumn lastModifiedDateColumn = table.getColumnModel().getColumn(3);
			table.getColumnModel().removeColumn(lastModifiedDateColumn);

			// Buttons
			JButton newPasswordButton = new JButton("New");
			newPasswordButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					newPassword();
				}
			});

			final JButton viewPasswordButton = new JButton("Details");
			viewPasswordButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					viewPassword();
				}
			});
			viewPasswordButton.setEnabled(false);

			final JButton editPasswordButton = new JButton("Edit");
			editPasswordButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					editPassword();
				}
			});
			editPasswordButton.setEnabled(false);

			final JButton deletePasswordButton = new JButton("Delete");
			deletePasswordButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					deletePassword();
				}
			});
			deletePasswordButton.setEnabled(false);

			/*
			 * Selection listener for passwords table to enable/disable action
			 * buttons accordingly
			 */
			class PasswordsTableSelectionListner implements
					ListSelectionListener {
				@Override
				public void valueChanged(ListSelectionEvent e) {
					if (e.getSource() != passwordsTable.getSelectionModel())
						return;
					if (passwordsTable.getSelectedRow() == -1) {
						// nothing is selected
						viewPasswordButton.setEnabled(false);
						editPasswordButton.setEnabled(false);
						deletePasswordButton.setEnabled(false);
					} else {
						if (!viewPasswordButton.isEnabled())
							viewPasswordButton.setEnabled(true);
						if (!editPasswordButton.isEnabled())
							editPasswordButton.setEnabled(true);
						if (!deletePasswordButton.isEnabled())
							deletePasswordButton.setEnabled(true);
					}
				}
			}
			table.getSelectionModel().addListSelectionListener(new PasswordsTableSelectionListner());

			// Panel to hold the buttons
			JPanel bp = new JPanel();
			bp.add(viewPasswordButton);
			bp.add(editPasswordButton);
			bp.add(newPasswordButton);
			bp.add(deletePasswordButton);

			// Add button panel to the tab
			tab.add(bp, PAGE_END);

		} else if (tableType.equals(KEYPAIRS)) { // Key Pairs tab
			// The Key Pairs table's data model
			KeyPairsTableModel keyPairsTableModel = new KeyPairsTableModel(credManager);
			// The table itself
			table = new JTable(keyPairsTableModel);

			/*
			 * Set the alias and service URIs columns of the KayPairs table to
			 * be invisible by removing them from the column model (they will
			 * still present in the table model)
			 * 
			 * Remove the last column first
			 */
			TableColumn aliasColumn = table.getColumnModel().getColumn(6);
			table.getColumnModel().removeColumn(aliasColumn);
			TableColumn serviceURIsColumn = table.getColumnModel().getColumn(5);
			table.getColumnModel().removeColumn(serviceURIsColumn);
			TableColumn lastModifiedDateColumn = table.getColumnModel().getColumn(4);
			table.getColumnModel().removeColumn(lastModifiedDateColumn);

			// Buttons
			final JButton viewKeyPairButton = new JButton("Details");
			viewKeyPairButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					viewCertificate();
				}
			});
			viewKeyPairButton.setEnabled(false);

			JButton importKeyPairButton = new JButton("Import");
			importKeyPairButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					importKeyPair();
				}
			});

			final JButton exportKeyPairButton = new JButton("Export");
			exportKeyPairButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					exportKeyPair();
				}
			});
			exportKeyPairButton.setEnabled(false);

			final JButton deleteKeyPairButton = new JButton("Delete");
			deleteKeyPairButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					deleteKeyPair();
				}
			});
			deleteKeyPairButton.setEnabled(false);

			/*
			 * Selection listener for key pairs table to enable/disable action
			 * buttons accordingly
			 */
			class KeyPairsTableSelectionListner implements
					ListSelectionListener {
				@Override
				public void valueChanged(ListSelectionEvent e) {
					if (e.getSource() != keyPairsTable.getSelectionModel())
						return;
					if (keyPairsTable.getSelectedRow() == -1) {
						// nothing is selected
						viewKeyPairButton.setEnabled(false);
						exportKeyPairButton.setEnabled(false);
						deleteKeyPairButton.setEnabled(false);
					} else {
						if (!viewKeyPairButton.isEnabled())
							viewKeyPairButton.setEnabled(true);
						if (!exportKeyPairButton.isEnabled())
							exportKeyPairButton.setEnabled(true);
						if (!deleteKeyPairButton.isEnabled())
							deleteKeyPairButton.setEnabled(true);
					}
				}
			}
			table.getSelectionModel().addListSelectionListener(
					new KeyPairsTableSelectionListner());

			// Panel to hold the buttons
			JPanel bp = new JPanel();
			bp.add(viewKeyPairButton);
			bp.add(importKeyPairButton);
			bp.add(exportKeyPairButton);
			bp.add(deleteKeyPairButton);

			// Add button panel to the tab
			tab.add(bp, PAGE_END);
		} else if (tableType.equals(TRUSTED_CERTIFICATES)) { // Certificates tab

			// The Trusted Certificate table's data model
			TrustedCertsTableModel trustedCertificatesTableModel = new TrustedCertsTableModel(credManager);
			// The table itself
			table = new JTable(trustedCertificatesTableModel);

			/*
			 * Set the alias columns of the Trusted Certs table to be invisible
			 * by removing them from the column model (they will still be
			 * present in the table model)
			 * 
			 * Remove the last column first
			 */
			TableColumn aliasColumn = table.getColumnModel().getColumn(5);
			table.getColumnModel().removeColumn(aliasColumn);
			TableColumn lastModifiedDateColumn = table.getColumnModel().getColumn(4);
			table.getColumnModel().removeColumn(lastModifiedDateColumn);

			// Buttons
			final JButton viewTrustedCertificateButton = new JButton("Details");
			viewTrustedCertificateButton
					.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							viewCertificate();
						}
					});
			viewTrustedCertificateButton.setEnabled(false);

			JButton importTrustedCertificateButton = new JButton("Import");
			importTrustedCertificateButton
					.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							importTrustedCertificate();
						}
					});

			final JButton exportTrustedCertificateButton = new JButton("Export");
			exportTrustedCertificateButton
					.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							exportTrustedCertificate();
						}
					});
			exportTrustedCertificateButton.setEnabled(false);

			final JButton deleteTrustedCertificateButton = new JButton("Delete");
			deleteTrustedCertificateButton
					.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							deleteTrustedCertificate();
						}
					});
			deleteTrustedCertificateButton.setEnabled(false);

			// Selection listener for trusted certs table to enable/disable action buttons accordingly
			class TrustedCertsTableSelectionListener implements
					ListSelectionListener {
				@Override
				public void valueChanged(ListSelectionEvent e) {
					if (e.getSource() != trustedCertsTable.getSelectionModel())
						return;
					if (trustedCertsTable.getSelectedRow() == -1) {
						// nothing is selected
						viewTrustedCertificateButton.setEnabled(false);
						exportTrustedCertificateButton.setEnabled(false);
						deleteTrustedCertificateButton.setEnabled(false);
					} else {
						if (!viewTrustedCertificateButton.isEnabled())
							viewTrustedCertificateButton.setEnabled(true);
						if (!exportTrustedCertificateButton.isEnabled())
							exportTrustedCertificateButton.setEnabled(true);
						if (!deleteTrustedCertificateButton.isEnabled())
							deleteTrustedCertificateButton.setEnabled(true);
					}
				}
			}
			table.getSelectionModel().addListSelectionListener(
					new TrustedCertsTableSelectionListener());

			// Panel to hold the buttons
			JPanel bp = new JPanel();
			bp.add(viewTrustedCertificateButton);
			bp.add(importTrustedCertificateButton);
			bp.add(exportTrustedCertificateButton);
			bp.add(deleteTrustedCertificateButton);

			// Add button panel to the tab
			tab.add(bp, PAGE_END);
		} else {
			throw new RuntimeException("Unknown table type " + tableType);
		}

		table.setShowGrid(false);
		table.setRowMargin(0);
		table.getColumnModel().setColumnMargin(0);
		table.getTableHeader().setReorderingAllowed(false);
		table.setAutoResizeMode(AUTO_RESIZE_ALL_COLUMNS);
		// Top accommodates entry icons with 2 pixels spare space (images are
		// 16x16 pixels)
		table.setRowHeight(18);

		// Add custom renderrers for the table headers and cells
		for (int iCnt = 0; iCnt < table.getColumnCount(); iCnt++) {
			TableColumn column = table.getColumnModel().getColumn(iCnt);
			column.setHeaderRenderer(new TableHeaderRenderer());
			column.setCellRenderer(new TableCellRenderer());
		}

		// Make the first column small and not resizable (it holds icons to
		// represent different entry types)
		TableColumn typeCol = table.getColumnModel().getColumn(0);
		typeCol.setResizable(false);
		typeCol.setMinWidth(20);
		typeCol.setMaxWidth(20);
		typeCol.setPreferredWidth(20);

		// Set the size for the second column
		// (i.e. Service URI column of Passwords table, and
		// Certificate Name column of the Kay Pairs and Trusted Certificates tables)
		// We do not care about the size of other columns.
		TableColumn secondCol = table.getColumnModel().getColumn(1);
		secondCol.setMinWidth(20);
		secondCol.setMaxWidth(10000);
		secondCol.setPreferredWidth(300);

		// Put the table into a scroll pane
		JScrollPane jspTableScrollPane = new JScrollPane(table,
				VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
		jspTableScrollPane.getViewport().setBackground(table.getBackground());

		// Put the scroll pane on the tab panel
		tab.add(jspTableScrollPane, CENTER);
		jspTableScrollPane.setBorder(new EmptyBorder(3, 3, 3, 3));

		/*
		 * Add mouse listeners to show an entry's details if it is
		 * double-clicked
		 */
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent evt) {
				tableDoubleClick(evt);
			}
		});

		// Add the tab to the tabbed pane
		keyStoreTabbedPane.addTab(tableType, tab);

		return table;
	}