PageRenderTime 66ms CodeModel.GetById 48ms app.highlight 15ms RepoModel.GetById 0ms app.codeStats 1ms

/bundles/plugins-trunk/InfoViewer/infoviewer/HelpInfoViewer.java

#
Java | 216 lines | 166 code | 31 blank | 19 comment | 18 complexity | ddc9a831651b14e3b4ee0fe0e9df9137 MD5 | raw file
  1package infoviewer;
  2
  3// import infoviewer.lucene.SearchPanel;
  4
  5import java.awt.BorderLayout;
  6import java.awt.Component;
  7import java.awt.Dimension;
  8import java.awt.event.ActionEvent;
  9import java.awt.event.ActionListener;
 10import java.io.File;
 11import java.net.MalformedURLException;
 12import java.net.URI;
 13import java.net.URL;
 14
 15import javax.swing.JSplitPane;
 16import javax.swing.JTabbedPane;
 17import javax.swing.SwingUtilities;
 18
 19import org.gjt.sp.jedit.MiscUtilities;
 20import org.gjt.sp.jedit.View;
 21import org.gjt.sp.jedit.jEdit;
 22import org.gjt.sp.jedit.help.HelpSearchPanel;
 23import org.gjt.sp.jedit.help.HelpTOCPanel;
 24import org.gjt.sp.jedit.help.HelpViewerInterface;
 25import org.gjt.sp.util.Log;
 26
 27/**
 28 * An infoviewer with a helpful side-bar. Reuses the same sidebar from the
 29 * original HelpViewer but is intended to be extended with other kinds of views
 30 * later.
 31 * 
 32 * @author ezust
 33 * @version $Id: HelpInfoViewer.java 20753 2012-01-12 04:14:36Z ezust $
 34 */
 35public class HelpInfoViewer extends InfoViewer implements HelpViewerInterface {
 36
 37	public void toggleSideBar() {
 38		showSideBar = aToggleSidebar.isSelected();
 39		remove(centralComponent);
 40		innerPanel.remove(centralComponent);
 41		if (showSideBar) 
 42		{
 43			splitter.setLeftComponent(tabs);
 44			splitter.setRightComponent(scrViewer);
 45			centralComponent = splitter;
 46			splitter.setDividerLocation(jEdit.getIntegerProperty("helpviewer.splitter", 250));
 47			
 48		} else 
 49		{
 50			int dl = splitter.getDividerLocation();
 51			if (dl > 0) jEdit.setIntegerProperty("helpviewer.splitter", dl);
 52			centralComponent = scrViewer;
 53		}
 54		
 55		innerPanel.add(BorderLayout.CENTER, centralComponent);
 56		scrViewer.repaint();
 57		repaint();
 58	}
 59
 60	public HelpInfoViewer() 
 61	{
 62		this(null, null, true);
 63	}
 64
 65	public HelpInfoViewer(View view, String position) 
 66	{
 67		this(view, position, true);
 68	}
 69	
 70	public HelpInfoViewer(View view, String position, boolean sidebar) 
 71	{
 72		super(view, position);
 73		if (sidebar) setName("helpviewer");
 74		showSideBar = sidebar;
 75		
 76		// Log.log(Log.WARNING, this.getClass(), "HelpInfoViewer!");
 77		tabs = new JTabbedPane();
 78		toc = new HelpTOCPanel(this);
 79		tabs.addTab(jEdit.getProperty("helpviewer.toc.label"), toc);
 80		searchPanel = new HelpSearchPanel(this);
 81		tabs.addTab(jEdit.getProperty("helpviewer.search.label"), searchPanel);
 82//		tabs.addTab("Lucene", new SearchPanel(this));
 83		tabs.setMinimumSize(new Dimension(0, 20));
 84		scrViewer.setMinimumSize(new Dimension(0, 20));
 85
 86		splitter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tabs, scrViewer);
 87		splitter.setBorder(null);
 88		if (showSideBar) {
 89			centralComponent = splitter;
 90			splitter.setDividerLocation(jEdit.getIntegerProperty("helpviewer.splitter", 250));
 91		}
 92		else 
 93		{
 94			centralComponent = scrViewer;
 95		}
 96		innerPanel.add(BorderLayout.CENTER, centralComponent);
 97		queueTOCReload();
 98		setVisible(true);
 99		SwingUtilities.invokeLater(new Runnable() 
100			{
101			public void run() 
102			{
103				splitter.setDividerLocation(jEdit.getIntegerProperty(
104						"helpviewer.splitter", 250));
105				requestFocus();
106			}
107		});
108	}
109
110	// {{{ dispose
111	public void dispose() {
112		int dl = splitter.getDividerLocation();
113		if (dl > 0) jEdit.setIntegerProperty("helpviewer.splitter", dl);		
114	} // }}}
115		
116	// {{{ queueTOCReload() method
117
118	public void queueTOCReload() 
119	{
120		SwingUtilities.invokeLater(new Runnable() 
121			{
122			public void run() {
123				queuedTOCReload = false;
124				toc.load();
125			}
126		});
127	} // }}}
128
129	// {{{ ActionHandler class
130	class ActionHandler implements ActionListener {
131		// {{{ actionPerformed() class
132		public void actionPerformed(ActionEvent evt) {
133			Object source = evt.getSource();
134			String url = evt.getActionCommand();
135			if (!url.equals("")) {
136				gotoURL(url, false, -1);
137				return;
138			}
139		} // }}}
140	} // }}}
141
142	public void setTitle(String newTitle) {
143		firePropertyChange("title", null, newTitle);
144	}
145
146	public Component getComponent() {
147		return this;
148	}
149
150	public void gotoURL(String url, boolean addToHistory, int scrollPos) {
151
152		if (baseURL == null) try {
153			URI baseURI = new File(MiscUtilities.constructPath(
154					jEdit.getJEditHome(), "doc")).toURI();
155			baseURL = baseURI.toURL().toString();
156		} catch (MalformedURLException mu) {
157			Log.log(Log.ERROR, this, mu);
158			// what to do?
159		}
160		if (url == null)
161			return;
162		url = url.trim();
163		if (url.length() == 0)
164			return;
165		String shortURL;
166		if(MiscUtilities.isURL(url))
167		{
168			if(url.startsWith(baseURL))
169			{
170				shortURL = url.substring(baseURL.length());
171				if(shortURL.startsWith("/"))
172					shortURL = shortURL.substring(1);
173			}
174			else
175			{
176				shortURL = url;
177			}
178		}
179		else
180		{
181			shortURL = url;
182			if(baseURL.endsWith("/"))
183				url = baseURL + url;
184			else
185				url = baseURL + '/' + url;
186		}
187		
188		try {
189			URL u = new URL(url);
190			gotoURL(u, addToHistory, scrollPos);
191		} catch (MalformedURLException mu) {
192			urlField.setText(url);
193			showError(jEdit.getProperty("infoviewer.error.badurl.message",
194					new Object[] { mu }));
195		}
196	}
197
198	// {{{ Private members
199
200	// {{{ Instance members
201	private JSplitPane splitter;
202
203	private JTabbedPane tabs;
204
205	private HelpSearchPanel searchPanel;
206
207	private HelpTOCPanel toc;
208
209	private boolean queuedTOCReload;
210
211	private boolean showSideBar = true;
212
213	private Component centralComponent;
214	// }}}
215
216}