View Javadoc

1   /*
2    * Created on Oct 20, 2005. Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the "License"); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.editor.xmlEditor.actions;
13  import java.util.Map;
14  import org.eclipse.jface.action.Action;
15  import org.eclipse.jface.dialogs.MessageDialog;
16  import org.eclipse.ui.IEditorPart;
17  import sk.uniba.euromath.document.XMLAccess;
18  import sk.uniba.euromath.document.NamespaceManager.NodeCount;
19  import sk.uniba.euromath.editor.MultiViewXMLEditor;
20  import sk.uniba.euromath.editor.actions.MultiViewActionContributor;
21  import sk.uniba.euromath.editor.xmlEditor.actions.lang.Messages;
22  /***
23   * Shows some info about current document - location, namespaces, number of
24   * elements and attributes, etc etc.
25   * @author Martin Vysny
26   */
27  public class ShowDocumentPropertiesAction extends Action {
28  	/***
29  	 * Owner contributor.
30  	 */
31  	private final MultiViewActionContributor contributor;
32  	/***
33  	 * Action constructor.
34  	 * @param contributor owner contributor.
35  	 */
36  	public ShowDocumentPropertiesAction(
37  			final MultiViewActionContributor contributor) {
38  		super(ActionConsts.SHOW_DOCUMENT_PROPERTIES_TEXT);
39  		this.contributor = contributor;
40  		// The id is used to refer to the action in a menu or toolbar
41  		setId(ActionConsts.SHOW_DOCUMENT_PROPERTIES_ACTION);
42  		// Associate the action with a pre-defined command, to allow key
43  		// bindings.
44  		setActionDefinitionId(ActionConsts.SHOW_DOCUMENT_PROPERTIES_ACTION);
45  	}
46  	/*
47  	 * (non-Javadoc)
48  	 * @see org.eclipse.jface.action.Action#run()
49  	 */
50  	@Override
51  	public void run() {
52  		final IEditorPart activeEditor = getContributor().getPage()
53  				.getActiveEditor();
54  		if (!(activeEditor instanceof MultiViewXMLEditor))
55  			return;
56  		final XMLAccess xml = ((MultiViewXMLEditor)activeEditor).getXMLAccess();
57  		// build a simple dialog text.
58  		final StringBuilder builder = new StringBuilder();
59  		builder.append(Messages.getString("ShowDocumentPropertiesAction.location")).append(xml.getDocumentURL().toString()) //$NON-NLS-1$
60  				.append("\n"); //$NON-NLS-1$
61  		builder.append(Messages.getString("ShowDocumentPropertiesAction.encoding")).append(xml.getEncoding()).append("\n\n"); //$NON-NLS-1$ //$NON-NLS-2$
62  		builder.append(Messages.getString("ShowDocumentPropertiesAction.namespaces")); //$NON-NLS-1$
63  		final Map<String, NodeCount> nodeCount = xml.getNsManager().getCounts();
64  		for (final String ns : nodeCount.keySet()) {
65  			builder.append(Messages.getString("ShowDocumentPropertiesAction.namespace")).append(ns).append("}\n"); //$NON-NLS-1$ //$NON-NLS-2$
66  			builder.append(Messages.getString("ShowDocumentPropertiesAction.prefixMapping")).append( //$NON-NLS-1$
67  					xml.getNsManager().getPrefix(ns)).append("\n"); //$NON-NLS-1$
68  			final NodeCount nc = nodeCount.get(ns);
69  			builder.append(Messages.getString("ShowDocumentPropertiesAction.elements")).append(nc.elements).append( //$NON-NLS-1$
70  					Messages.getString("ShowDocumentPropertiesAction.attributes")).append(nc.attributes).append("\n\n"); //$NON-NLS-1$ //$NON-NLS-2$
71  		}
72  		// show the dialog.
73  		MessageDialog.openInformation(getContributor().getPage()
74  				.getWorkbenchWindow().getShell(), xml.getFileName()
75  				+ Messages.getString("ShowDocumentPropertiesAction.properties"), builder.toString()); //$NON-NLS-1$
76  	}
77      /***
78       * @return Returns the contributor.
79       */
80      protected MultiViewActionContributor getContributor() {
81          return this.contributor;
82      }
83  }