PageRenderTime 186ms CodeModel.GetById 174ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/BufferTabs/buffertabs/BufferTabComponent.java

#
Java | 131 lines | 95 code | 17 blank | 19 comment | 3 complexity | 3d3b1157569048be2c7c12d48e617aa0 MD5 | raw file
  1/*
  2 * :tabSize=8:indentSize=8:noTabs=false:
  3 * :folding=explicit:collapseFolds=1:
  4 *
  5 * Copyright (C) 2009 Shlomy Reinstein
  6 *
  7 * This program is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU General Public License
  9 * as published by the Free Software Foundation; either version 2
 10 * of the License, or any later version.
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 19 */
 20package buffertabs;
 21
 22import java.awt.Color;
 23import java.awt.Component;
 24import java.awt.Dimension;
 25import java.awt.FlowLayout;
 26import java.awt.Graphics;
 27import java.awt.event.MouseAdapter;
 28import java.awt.event.MouseEvent;
 29
 30import javax.swing.Icon;
 31import javax.swing.JLabel;
 32import javax.swing.JPanel;
 33
 34import org.gjt.sp.jedit.EditPane;
 35import org.gjt.sp.jedit.jEdit;
 36
 37@SuppressWarnings("serial")
 38class BufferTabComponent extends JPanel
 39{
 40	private static CloseIcon icon = new CloseIcon();
 41	private static Dimension iconDimension =
 42		new Dimension(icon.getIconWidth(), icon.getIconHeight());
 43	private BufferTabs pane;
 44	
 45	BufferTabComponent(BufferTabs bufferTabs) {
 46		super(new FlowLayout(FlowLayout.CENTER, 5, 0));
 47		pane = bufferTabs;
 48		setOpaque(false);
 49		JLabel l = new BufferTabLabel(this);
 50		add(l);
 51		JLabel close = new JLabel(icon);
 52		close.setPreferredSize(iconDimension);
 53		close.setForeground(Color.BLACK);
 54		add(close);
 55		close.addMouseListener(new BufferTabCloseButtonListener(this));
 56	}
 57	
 58	private static class CloseIcon implements Icon {
 59		private static final int width = 9;
 60		private static final int height = 11;
 61		private static final int top = 3;
 62		public void paintIcon(Component c, Graphics g, int x, int y) {
 63			g.drawLine(0, top, width - 2, height - 1);
 64			g.drawLine(1, top, width - 1, height - 1);
 65			g.drawLine(width - 1, top, 1, height - 1);
 66			g.drawLine(width - 2, top, 0, height - 1);
 67		}
 68		public int getIconWidth() {
 69			return width;
 70		}
 71		public int getIconHeight() {
 72			return height;
 73		}
 74	}
 75	
 76	private class BufferTabCloseButtonListener extends MouseAdapter {
 77		
 78		private BufferTabComponent component;
 79		
 80		BufferTabCloseButtonListener(BufferTabComponent component) {
 81			this.component = component;
 82		}
 83		
 84		@Override
 85		public void mouseEntered(MouseEvent e) {
 86			JLabel close = (JLabel)e.getSource();
 87			close.setForeground(Color.RED);
 88		}
 89
 90		@Override
 91		public void mouseExited(MouseEvent e) {
 92			JLabel close = (JLabel)e.getSource();
 93			close.setForeground(Color.BLACK);
 94		}
 95
 96		@Override
 97		public void mouseClicked(MouseEvent e) {
 98			int index = pane.indexOfTabComponent(component);
 99			if (index < 0)
100				return;
101			EditPane editPane = pane.getEditPane();
102			jEdit.closeBuffer(editPane,
103				editPane.getBufferSet().getBuffer(index));
104		}
105	}
106	
107	private class BufferTabLabel extends JLabel {
108		
109		private BufferTabComponent component;
110		
111		BufferTabLabel(BufferTabComponent component) {
112			this.component = component;
113		}
114		
115		@Override
116		public Icon getIcon() {
117			int index = pane.indexOfTabComponent(component);
118			if (index < 0)
119				return null;
120			return pane.getIconAt(index);
121		}
122		@Override
123		public String getText() {
124			int index = pane.indexOfTabComponent(component);
125			if (index < 0)
126				return null;
127			return pane.getTitleAt(index);
128		}
129	}
130
131}