⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtexttabbedpaneview.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 *
	 * @param index The tab for which you want to get the
	 *        {@link org.fife.rtext.RTextEditorPane}.
	 * @return The corresponding {@link org.fife.rtext.RTextEditorPane}.
	 */
	public RTextEditorPane getRTextEditorPaneAt(int index) {
		if (index<0 || index>=getNumDocuments())
			//throw new IndexOutOfBoundsException();
			return null;
		return (RTextEditorPane)((RTextScrollPane)tabbedPane.
									getComponentAt(index)).textArea;
	}


/*****************************************************************************/


	/**
	 * Returns the scroll pane at the specified tab.
	 *
	 * @param index The tab for which you want to get the scroll pane.
	 * @return The scroll pane.
	 */
	public RTextScrollPane getRTextScrollPaneAt(int index) {
		if (index<0 || index>=getNumDocuments())
			//throw new IndexOutOfBoundsException();
			return null;
		return (RTextScrollPane)tabbedPane.getComponentAt(index);
	}


/*****************************************************************************/


	/**
	 * Returns the currently active component.
	 *
	 * @return The component.
	 */
	public Component getSelectedComponent() {
		return tabbedPane.getComponentAt(tabbedPane.getSelectedIndex());
	}


/*****************************************************************************/


	/**
	 * Returns the currently selected document's index.
	 *
	 * @return The index of the currently selected document.
	 */
	public int getSelectedIndex() {
		return tabbedPane.getSelectedIndex();
	}


/*****************************************************************************/


	/**
	 * Repaints the display names for open documents.
	 */
	public void refreshDisplayNames() {

		Color defaultForeground = UIManager.getColor("tabbedpane.foreground");
		Color modifiedColor = getModifiedDocumentDisplayNamesColor();
		int numDocuments = getNumDocuments();

		if (highlightModifiedDocumentDisplayNames()==true) {
			for (int i=0; i<numDocuments; i++) {
				if (getRTextEditorPaneAt(i).isModified()==true)
					tabbedPane.setForegroundAt(i, modifiedColor);
				else
					tabbedPane.setForegroundAt(i, defaultForeground);
			}
		}
		else {
			for (int i=0; i<numDocuments; i++)
				tabbedPane.setForegroundAt(i, defaultForeground);
		}

	}


/*****************************************************************************/


	/**
	 * Removes a change listener from this tabbed pane.
	 *
	 * @param listener The listener to remove.
	 */
	public void removeChangeListener(ChangeListener listener) {
		tabbedPane.removeChangeListener(listener);
	}


/*****************************************************************************/


	/**
	 * Removes a component from this container.  Note that this method does
	 * not update currentTextArea, you must do that yourself.
	 */
	protected void removeComponentAt(int index) {
		if (index>=0 && index<getNumDocuments()) {
			tabbedPane.removeTabAt(index);
			//currentTextArea = getRTextEditorPaneAt(getSelectedIndex());
		}
	}


/*****************************************************************************/


	/**
	 * Removes a container listener from this tabbed pane.
	 *
	 * @param listener The listener to remove.
	 */
	public void removeContainerListener(ContainerListener listener) {
		tabbedPane.removeContainerListener(listener);
	}


/*****************************************************************************/


	/**
	 * Sets the name of the document displayed on the document's tab.
	 *
	 * @param index The index of the document whose name you want to change.
	 *        If this value is invalid, this method does nothing.
	 * @param displayName The name to display.
	 * @see #getDocumentDisplayNameAt
	 */
	public void setDocumentDisplayNameAt(int index, String displayName) {
		if (index>=0 && index<getNumDocuments()) {
			tabbedPane.setTitleAt(index, displayName);
			// Hack-of-a-way to tell if this document is modified.
			if (displayName.charAt(displayName.length()-1)=='*') {
				if (highlightModifiedDocumentDisplayNames()==true) {
					tabbedPane.setForegroundAt(index,
							getModifiedDocumentDisplayNamesColor());
				}
				else {
					tabbedPane.setForegroundAt(index,
										tabbedPane.getForeground());
				}
			}
			// Just set it to regular color (this may/may not be unnecessary...).
			else {
				tabbedPane.setForegroundAt(index, tabbedPane.getForeground());
			}
		}
		// May need to reset icon if extension has changed.
		tabbedPane.setIconAt(index,
				getIconFor((RTextScrollPane)getRTextScrollPaneAt(index)));
	}


/*****************************************************************************/


	/**
	 * Changes the location of the document selection area of this component.
	 *
	 * @param location The location to use; (<code>TOP</code>,
	 *        <code>LEFT</code>, <code>BOTTOM</code>, or <code>RIGHT</code>.
	 *        If this value is invalid, nothing happens.
	 */
	 public void setDocumentSelectionPlacement(int location) {
		if (location==DOCUMENT_SELECT_TOP || location==DOCUMENT_SELECT_LEFT ||
			location==DOCUMENT_SELECT_BOTTOM || location==DOCUMENT_SELECT_RIGHT)
			tabbedPane.setTabPlacement(location);
	}


/*****************************************************************************/


	/**
	 * Sets the currently active document.  This updates currentTextArea.
	 *
	 * @param index The index of the document to make the active document.
	 *        If this value is invalid, nothing happens.
	 */
	public void setSelectedIndex(int index) {
		if (index>=0 && index<getNumDocuments()) {
			tabbedPane.setSelectedIndex(index);
			currentTextArea = getRTextEditorPaneAt(index);
			updateStatusBar();
			currentTextArea.requestFocusInWindow();
		}
	}


/*****************************************************************************/


	public void stateChanged(ChangeEvent e) {

		// Skip this stuff if we're called from closeCurrentDocument(), as
		// that method does this stuff to; let's not do it twice (listeners
		// would get two CURRENT_DOCUMENT_PROPERTY events).
		if (inCloseCurrentDocument)
			return;

		// TODO: Factor the code below and the similar code in
		// closeCurrentDocument() into a common method.

		// Remove the RText listeners associated with the current
		// currentTextArea.
		if (currentTextArea!=null) {
			currentTextArea.getDocument().removeDocumentListener(owner);
			currentTextArea.removeCaretListener(owner);
			currentTextArea.removeKeyListener(owner);
		}

		// The new currentTextArea will only be null when we're closing the
		// only open document.  Even then, after this a new document will be
		// opened and this method will be re-called.
		currentTextArea = getRTextEditorPaneAt(getSelectedIndex());
		if (currentTextArea!=null) {

			if (currentTextArea.isModified())
				owner.setMessages(currentTextArea.getFileFullPath() + "*", null);
			else
				owner.setMessages(currentTextArea.getFileFullPath(), null);
			updateStatusBar(); // Update read-only and line/col. indicators.

			// Give the current text area focus.  We have to do this in
			// a Runnable as during this stateChanged() call, the text area's
			// panel hasn't actually been made visible yet, and that must
			// have happened for requestFocusInWindow to work.
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					// We check for null because this is also called when
					// we switch view styles, and will throw a NPE in that
					// case as all of the text areas have been removed.
					if (currentTextArea!=null)
						currentTextArea.requestFocusInWindow();
				}
			});

			// Update RText actions associated with the current currentTextArea.
			currentTextArea.getDocument().addDocumentListener(owner);
			currentTextArea.addCaretListener(owner);
			currentTextArea.addKeyListener(owner);
			
			// Trick the parent RText into updating the row/column indicator.
			// We have to check mainView for null because this is called in
			// RText's constructor, before RText has a mainView.
			if (owner.getMainView()!=null)
				// Null because caretUpdate doesn't actually use the caret event.
				owner.caretUpdate(null);

			// Let any listeners know that the current document changed.
			firePropertyChange(CURRENT_DOCUMENT_PROPERTY, -1, getSelectedIndex());
			fireCurrentTextAreaEvent(CurrentTextAreaEvent.TEXT_AREA_CHANGED,
								null, currentTextArea);

		}

	}


/*****************************************************************************/
/******************* PRIVATE INNER CLASSES ***********************************/
/*****************************************************************************/


	/**
	 * The actual tabbed pane.
	 */
	class TabbedPane extends JTabbedPane
							implements DrawDnDIndicatorTabbedPane {

		private int x,y, width,height;
		private Stroke wideStroke;

		public TabbedPane() {
			ToolTipManager.sharedInstance().registerComponent(this);
		}

		public void clearDnDIndicatorRect() {
			x = y = -1;
			repaint();
		}

		public String getToolTipText(MouseEvent e) {
			TabbedPaneUI ui = getUI();
			if (ui != null) {
				int index = ui.tabForCoordinate(this, e.getX(), e.getY());
				if (index!=-1)
					return getRTextEditorPaneAt(index).getFileFullPath();
			}
			return super.getToolTipText(e);
		}

		public void setDnDIndicatorRect(int x, int y, int width, int height) {
			this.x = x; this.y = y;
			this.width = width; this.height = height;
			repaint();
		}

		public void paint(Graphics g) {
			super.paint(g);
			if (x!=-1) {
				// Sine this must be the EDT we can lazily create.
				if (wideStroke==null)
					wideStroke = new BasicStroke(2.0f);
				Graphics2D g2d = (Graphics2D)g;
				Stroke temp = g2d.getStroke();
				g2d.setStroke(wideStroke);
				g2d.setColor(SystemColor.controlDkShadow);
				g2d.drawRect(x,y, width,height);
				g2d.setStroke(temp);
			}
		}

	}


/*****************************************************************************/


	/**
	 * Listens for the user drag-and-dropping tabs in this tabbed
	 * pane.
	 */
	protected class TabDragListener extends MouseInputAdapter {

		private int tab;
		private JComponent draggedTab;
		MouseEvent firstMouseEvent;

		public void mousePressed(MouseEvent e) {
			tab = tabbedPane.indexAtLocation(e.getX(), e.getY());
			if (tab>-1) {
				draggedTab = (JComponent)tabbedPane.getComponentAt(tab);
				firstMouseEvent = e;
				e.consume();
			}
		}

		public void mouseMoved(MouseEvent e) {
		}

		public void mouseDragged(MouseEvent e) {
			if (draggedTab==null)
				return;
			if (firstMouseEvent != null) {
				e.consume();
				int action = javax.swing.TransferHandler.MOVE;
				int dx = Math.abs(e.getX() - firstMouseEvent.getX());
				int dy = Math.abs(e.getY() - firstMouseEvent.getY());
				//Arbitrarily define a 5-pixel shift as the
				//official beginning of a drag.
				if (dx > 5 || dy > 5) {
					//This is a drag, not a click.
					//Tell the transfer handler to initiate the drag.
					transferHandler.exportAsDrag(tabbedPane,
											firstMouseEvent, action);
					firstMouseEvent = null;
				}
			}
		}

		public void mouseReleased(MouseEvent e) {
			draggedTab = null;
			firstMouseEvent = null;
		}

	}


/*****************************************************************************/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -