public void actionPerformed()

in taverna-perspective-myexperiment/src/main/java/org/apache/taverna/ui/perspectives/myexperiment/UploadWorkflowDialog.java [489:745]


	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(this.bUpload)) { // * *** *** *** * UPLOAD
													// BUTTON *
			// *** *** *** *
			// perform source check returns a file if attaining the source was
			// successful
			if (userRequestedWorkflowUpload) {
				uploadFile = performSourceCheck();
				if (uploadFile == null)
					return;
			}

			// collect and put the metadata values in their respectable vars
			getMetadata();

			// if the description or the title are empty, prompt the user to
			// confirm
			// the upload
			boolean proceedWithUpload = false;
			if ((this.strDescription.length() == 0) && (this.strTitle.length() == 0)) {
				String strInfo = "The workflow 'title' field and the 'description' field\n"
						+ "(or both) are empty.  Any metadata found within the\n"
						+ "workflow will be used instead.  Do you wish to proceed?";
				int confirm = JOptionPane.showConfirmDialog(this, strInfo, "Empty fields",
						JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
				if (confirm == JOptionPane.YES_OPTION)
					proceedWithUpload = true;
			} else
				proceedWithUpload = true;

			if (proceedWithUpload) {
				// the window will stay visible, but should turn into 'waiting'
				// state
				final JRootPane rootPane = this.getRootPane();
				final Container contentPane = this.getContentPane();
				contentPane.remove(this.bUpload);
				contentPane.remove(this.bCancel);
				if (this.lStatusMessage != null)
					contentPane.remove(this.lStatusMessage);
				this.taDescription.setEditable(false);

				final GridBagConstraints c = new GridBagConstraints();
				c.gridx = 0;
				c.gridy = gridYPositionForStatusLabel;
				c.gridwidth = 2;
				c.anchor = GridBagConstraints.CENTER;
				c.fill = GridBagConstraints.NONE;
				c.insets = new Insets(10, 5, 10, 5);
				lStatusMessage = new JLabel((updateResource == null ? "Uploading" : "Updating")
						+ " your workflow...", new ImageIcon(
						MyExperimentPerspective.getLocalResourceURL("spinner")),
						SwingConstants.CENTER);
				contentPane.add(lStatusMessage, c);

				// disable the (X) button (ideally, would need to remove it, but
				// there's
				// no way to do this)
				this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

				// revalidate the window
				this.pack();
				this.validate();
				this.repaint();

				new Thread("Posting workflow") {
					boolean formatRecognized = false;

					@Override
					public void run() {
						String workflowFileContent = "";
						if (userRequestedWorkflowUpload) {
							if (uploadFile != null) {
								try {
									BufferedReader reader = new BufferedReader(new FileReader(
											uploadFile));
									String line;

									String scuflSchemaDef = "xmlns:s=\"http://org.embl.ebi.escience/xscufl/0.1alpha\"";
									String t2flowSchemaDef = "xmlns=\"http://taverna.sf.net/2008/xml/t2flow\"";

									while ((line = reader.readLine()) != null) {
										if (!formatRecognized
												&& (line.contains(scuflSchemaDef) || line
														.contains(t2flowSchemaDef)))
											formatRecognized = true;

										workflowFileContent += line + "\n";
									}
								} catch (Exception e) {
									lStatusMessage = new JLabel("Error occurred:" + e.getMessage(),
											new ImageIcon(MyExperimentPerspective
													.getLocalResourceURL("failure_icon")),
											SwingConstants.LEFT);
									logger.error(e.getCause() + "\n" + e.getMessage());
								}
							}
						}

						// *** POST THE WORKFLOW ***
						final ServerResponse response;

						if ((userRequestedWorkflowUpload && formatRecognized)
								|| !userRequestedWorkflowUpload) {
							if (updateResource == null) // upload a new workflow
								response = myExperimentClient.postWorkflow(workflowFileContent,
										Util.stripAllHTML(strTitle),
										Util.stripAllHTML(strDescription), licence, sharing);
							else
								// edit existing workflow
								response = myExperimentClient.updateWorkflowVersionOrMetadata(
										updateResource, workflowFileContent,
										Util.stripAllHTML(strTitle),
										Util.stripAllHTML(strDescription), licence, sharing);

							bUploadingSuccessful = (response.getResponseCode() == HttpURLConnection.HTTP_OK);
						} else {
							bUploadingSuccessful = false;
							response = null;
						}

						SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								// *** REACT TO POSTING RESULT ***
								if (bUploadingSuccessful) {
									// workflow uploaded successfully
									setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

									// disable all fields in dialog
									tfTitle.setEnabled(false);
									taDescription.setEnabled(false);
									jcbLicences.setEnabled(false);
									jcbSharingPermissions.setEnabled(false);
									if (userRequestedWorkflowUpload) {
										rbSelectOpenWorkflow.setEnabled(false);
										rbSelectLocalFile.setEnabled(false);
										selectedFileLabel.setEnabled(false);
										bSelectFile.setEnabled(false);
										jcbOpenWorkflows.setEnabled(false);
									}
									contentPane.remove(lStatusMessage);

									c.insets = new Insets(10, 5, 5, 5);
									lStatusMessage = new JLabel("Your workflow was successfully "
											+ (updateResource == null ? "uploaded." : "updated."),
											new ImageIcon(MyExperimentPerspective
													.getLocalResourceURL("success_icon")),
											SwingConstants.LEFT);
									contentPane.add(lStatusMessage, c);

									bCancel.setText("OK");
									bCancel.setDefaultCapable(true);
									rootPane.setDefaultButton(bCancel);
									c.insets = new Insets(5, 5, 10, 5);
									c.gridy++;
									contentPane.add(bCancel, c);

									pack();
									bCancel.requestFocusInWindow();

									// update uploaded items history making sure
									// that:
									// - there's only one occurrence of this
									// item in the history;
									// - if this item was in the history before,
									// it is moved to
									// the 'top' now;
									// - predefined history size is not exceeded
									MainComponent.MAIN_COMPONENT.getHistoryBrowser()
											.getUploadedItemsHistoryList().remove(updateResource);
									MainComponent.MAIN_COMPONENT.getHistoryBrowser()
											.getUploadedItemsHistoryList().add(updateResource);
									if (MainComponent.MAIN_COMPONENT.getHistoryBrowser()
											.getUploadedItemsHistoryList().size() > HistoryBrowserTabContentPanel.UPLOADED_ITEMS_HISTORY_LENGTH) {
										MainComponent.MAIN_COMPONENT.getHistoryBrowser()
												.getUploadedItemsHistoryList().remove(0);
									}

									// now update the uploaded items history
									// panel in 'History'
									// tab
									if (MainComponent.MAIN_COMPONENT.getHistoryBrowser() != null) {
										MainComponent.MAIN_COMPONENT
												.getHistoryBrowser()
												.refreshHistoryBox(
														HistoryBrowserTabContentPanel.UPLOADED_ITEMS_HISTORY);
									}

								} else {
									// posting wasn't successful, notify the
									// user
									// and provide an option to close window or
									// start again
									setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
									taDescription.setEditable(true);
									contentPane.remove(lStatusMessage);

									c.insets = new Insets(10, 5, 5, 5);

									String msg;
									if (!formatRecognized)
										msg = "Error occured: Invalid Taverna workflow.";
									else
										msg = "An error occured while processing your request.";

									lStatusMessage = new JLabel(msg, new ImageIcon(
											MyExperimentPerspective
													.getLocalResourceURL("failure_icon")),
											SwingConstants.LEFT);
									contentPane.add(lStatusMessage, c);

									bUpload.setText("Try again");
									bUpload.setToolTipText("Please review your workflow or myExperiment base URL");
									c.anchor = GridBagConstraints.EAST;
									c.insets = new Insets(5, 5, 10, 5);
									c.gridwidth = 1;
									c.weightx = 0.5;
									c.gridx = 0;
									c.gridy++;
									contentPane.add(bUpload, c);
									rootPane.setDefaultButton(bUpload);

									c.anchor = GridBagConstraints.WEST;
									c.gridx = 1;
									bCancel.setPreferredSize(bUpload.getPreferredSize());
									contentPane.add(bCancel, c);

									pack();
									validate();
									repaint();
								}
							}
						});
					}
				}.start();
			} // if proceedWithUpload
		} else if (e.getSource().equals(this.bCancel)) { // * CANCEL BUTTON *
			// cleanup the input fields if it wasn't posted successfully +
			// simply
			// close and destroy the window
			if (!this.bUploadingSuccessful) {
				this.strDescription = null;
				this.tfTitle = null;
			}
			this.dispose();
		} else if (e.getSource().equals(bSelectFile)) {// * SELECT FILE BUTTON *
			// *
			if (jfsSelectFile.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
				localWorkflowFile = jfsSelectFile.getSelectedFile();

				if (localWorkflowFile != null) {
					selectedFileLabel.setText(localWorkflowFile.getAbsolutePath());
					selectedFileLabel.setEnabled(true);
				}
				pack();
			}
		}
	}