private JPanel createActivityConfigurationPanel()

in taverna-xpath-activity-ui/src/main/java/org/apache/taverna/activities/xpath/ui/config/XPathActivityConfigurationPanel.java [202:479]


	private JPanel createActivityConfigurationPanel() {
		JPanel jpConfig = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		// text area for example XML document
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.5;
		c.weighty = 1.0;
		c.insets = new Insets(5, 0, 0, 5);
		taSourceXML = new JTextArea(10, 30);
		taSourceXML
				.setToolTipText("<html>Use this text area to paste or load an example XML document.<br>"
						+ "This document can then be parsed by clicking the button<br>"
						+ "with a green arrow in order to see its tree structure.</html>");
		taSourceXML.setText(EXAMPLE_XML_PROMPT);
		taSourceXML.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				taSourceXML.selectAll();
			}

			public void focusLost(FocusEvent e) { /* do nothing */
			}
		});
		taSourceXML.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				// make sure that it is only allowed to "parse example XML"
				// when something is actually present in the text area
				bParseXML.setEnabled(taSourceXML.getText().trim().length() > 0
						&& !taSourceXML.getText().trim().equals(
								EXAMPLE_XML_PROMPT));
			}
		});
		jpLeft = new JPanel(new GridLayout(1, 1));
		jpLeft.add(new JScrollPane(taSourceXML));
		jpConfig.add(jpLeft, c);

		// button to parse example XML document

		c.gridx++;
		c.fill = GridBagConstraints.NONE;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(0, 0, 0, 0);
		bParseXML = new JButton(
				XPathActivityIcon
						.getIconById(XPathActivityIcon.XPATH_ACTIVITY_CONFIGURATION_PARSE_XML_ICON));
		bParseXML
				.setToolTipText("Parse example XML document and generate its tree structure");
		bParseXML.setEnabled(false);
		bParseXML.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				parseXML();
			}
		});
		jpConfig.add(bParseXML, c);

		// placeholder for XML tree (will be replaced by a real tree when the
		// parsing is done)

		c.gridx++;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.5;
		c.weighty = 1.0;
		c.insets = new Insets(5, 5, 0, 0);
		JTextArea taXMLTreePlaceholder = new JTextArea(10, 30);
		taXMLTreePlaceholder
				.setToolTipText("<html>This area will show tree structure of the example XML after you<br>"
						+ "paste it into the space on the left-hand side and press 'Parse'<br>"
						+ "button with the green arrow.</html>");
		taXMLTreePlaceholder.setEditable(false);
		taXMLTreePlaceholder.setBackground(INACTIVE_PANEL_BACKGROUND_COLOR);
		spXMLTreePlaceholder = new JScrollPane(taXMLTreePlaceholder);
		jpRight = new JPanel(new GridLayout(1, 1));
		jpRight.add(spXMLTreePlaceholder);
		jpConfig.add(jpRight, c);

		// Button to load XML document from a file
		
		bLoadXMLDocument = new JButton("Load XML from file", WorkbenchIcons.openIcon);	
		bLoadXMLDocument.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				Preferences prefs = Preferences.userNodeForPackage(getClass());
				String curDir = prefs.get(XPATH_XML_DOCUMENT_DIR_PROPERTY, System.getProperty("user.home"));
				fileChooser.setDialogTitle("Select file to load XML from");
				fileChooser.setFileFilter(new FileFilter() {  
					public boolean accept(File f) {
				        return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml");
				    }
				    
				    public String getDescription() {
				        return ".xml files";
				    }
				});
				fileChooser.setCurrentDirectory(new File(curDir));		
				int returnVal = fileChooser.showOpenDialog(((JButton) e
						.getSource()).getParent());
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					prefs.put(XPATH_XML_DOCUMENT_DIR_PROPERTY, fileChooser
							.getCurrentDirectory().toString());
					File file = fileChooser.getSelectedFile();
					// Read the contents of a file into a string
					// and set the value of the XML document text area to it
					FileInputStream fis = null;
					try{
						byte[] fileBytes = new byte[(int)file.length()];
						fis = new FileInputStream(file);
						fis.read(fileBytes);
						String xmlDocument = new String(fileBytes, "UTF-8");
						setSourceXML(xmlDocument);
					}
					catch(Exception ex){
						logger.error("An error occured while trying to read the XML document from file " + file.getAbsolutePath(), ex);
						JOptionPane.showMessageDialog(
								((JButton) e.getSource()).getParent(), 
								"There was an error while trying to read the file", 
								"XPath Activity", 
								JOptionPane.ERROR_MESSAGE);
					}
					finally{
						try {
							fis.close();
						} catch (IOException e1) {
							// Ignore
						}
					}

				}
			}
		});
		c.gridx = 0;
		c.gridy++;
		c.gridwidth = 1;
		c.fill = GridBagConstraints.NONE;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(5, 0, 0, 5);
		c.anchor = GridBagConstraints.EAST;
		jpConfig.add(bLoadXMLDocument, c);
		
		// settings for the view of XML tree from example XML document

		miIncludeAttributes = new JCheckBoxMenuItem("Show XML node attributes");
		miIncludeAttributes.setSelected(true);
		miIncludeAttributes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				refreshXMLTreeUI();
			}
		});

		miIncludeValues = new JCheckBoxMenuItem(
				"Show values of XML elements and attributes");
		miIncludeValues.setSelected(true);
		miIncludeValues.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				refreshXMLTreeUI();
			}
		});

		miIncludeNamespaces = new JCheckBoxMenuItem(
				"Show namespaces of XML elements");
		miIncludeNamespaces.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				refreshXMLTreeUI();
			}
		});

		jpXMLTreeSettingsMenuContents = new JPanel();
		jpXMLTreeSettingsMenuContents.setBorder(BorderFactory
				.createRaisedBevelBorder());
		jpXMLTreeSettingsMenuContents.setLayout(new BoxLayout(
				jpXMLTreeSettingsMenuContents, BoxLayout.Y_AXIS));
		jpXMLTreeSettingsMenuContents.add(miIncludeAttributes);
		jpXMLTreeSettingsMenuContents.add(miIncludeValues);
		jpXMLTreeSettingsMenuContents.add(miIncludeNamespaces);

		bShowXMLTreeSettings = new JToggleButton("Show XML tree settings...",
				XPathActivityIcon.getIconById(XPathActivityIcon.UNFOLD_ICON));
		bShowXMLTreeSettings.setSelectedIcon(XPathActivityIcon
				.getIconById(XPathActivityIcon.FOLD_ICON));
		bShowXMLTreeSettings.setEnabled(false);
		bShowXMLTreeSettings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (xmlTreeSettingsMenu == null) {
					xmlTreeSettingsMenuLastShownAt = System.currentTimeMillis();

					Point parentPosition = bShowXMLTreeSettings
							.getLocationOnScreen();
					xmlTreeSettingsMenu = PopupFactory.getSharedInstance()
							.getPopup(
									bShowXMLTreeSettings,
									jpXMLTreeSettingsMenuContents,
									parentPosition.x,
									parentPosition.y
											+ bShowXMLTreeSettings.getHeight());
					xmlTreeSettingsMenu.show();
				} else {
					bShowXMLTreeSettings.setSelected(false);
				}
			}
		});
		
		bGenerateXPathExpression = new JButton("Generate XPath expression",
				XPathActivityIcon
						.getIconById(XPathActivityIcon.XML_TREE_NODE_ICON));
		bGenerateXPathExpression.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			     updateXPathEditingPanelValues();	
			}	
		});
		
		JPanel xmlTreeButtonPanel = new JPanel();
		xmlTreeButtonPanel.add(bGenerateXPathExpression);
		xmlTreeButtonPanel.add(bShowXMLTreeSettings);
		
		c.gridx = 2;
		c.gridwidth = 1;
		c.fill = GridBagConstraints.NONE;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(5, 0, 0, 0);
		c.anchor = GridBagConstraints.EAST;
		jpConfig.add(xmlTreeButtonPanel, c);

		// register a new listener for all AWT mouse events - this will be used
		// to identify clicks outside of the XML tree popup menu and the toggle
		// button used to show/hide it
		Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
			public void eventDispatched(AWTEvent event) {
				if (event instanceof MouseEvent && xmlTreeSettingsMenu != null) {
					MouseEvent e = (MouseEvent) event;
					if (e.getClickCount() > 0
							&& (e.getWhen() - xmlTreeSettingsMenuLastShownAt) > 100) {
						// convert a point where mouse click was made from
						// relative coordinates of the source component
						// to the coordinates of the panel that represents the
						// contents of the popup menu
						Point clickRelativeToOverlay = SwingUtilities
								.convertPoint((Component) e.getSource(), e
										.getPoint(),
										jpXMLTreeSettingsMenuContents);

						Area areaOfPopupPanelAndToggleButton = new Area(
								jpXMLTreeSettingsMenuContents.getBounds());

						// only hide the popup menu if a click was made outside
						// of the calculated area --
						// plus not on one of the associated toggle buttons
						if (!areaOfPopupPanelAndToggleButton
								.contains(clickRelativeToOverlay)) {
							xmlTreeSettingsMenu.hide();
							bShowXMLTreeSettings.setSelected(false);

							// if the popup menu was dismissed by a click on the
							// toggle button that
							// has made it visible, this timer makes sure that
							// this click doesn't
							// re-show the popup menu
							new Timer(100, new ActionListener() {
								public void actionPerformed(ActionEvent e) {
									((Timer) e.getSource()).stop();
									xmlTreeSettingsMenu = null;
								}
							}).start();

						}
					}
				}
			}
		}, AWTEvent.MOUSE_EVENT_MASK);

		return (jpConfig);
	}