source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/mainframe/tab/JTabbedPaneWithCloseIcons.java @ 6959

Last change on this file since 6959 was 6959, checked in by mlg, 14 years ago

Added a managed preference :
the ability for the user to choose the image quality (the gui is not currently done, so it is not usable)

File size: 4.3 KB
Line 
1package fr.mael.jiwigo.ui.mainframe.tab;
2
3import java.awt.Color;
4import java.awt.Component;
5import java.awt.Graphics;
6import java.awt.Rectangle;
7import java.awt.event.ActionEvent;
8import java.awt.event.ActionListener;
9import java.awt.event.MouseEvent;
10import java.awt.event.MouseListener;
11import java.util.ArrayList;
12
13import javax.swing.Icon;
14import javax.swing.JMenuItem;
15import javax.swing.JPopupMenu;
16import javax.swing.JTabbedPane;
17import javax.swing.SwingUtilities;
18
19import fr.mael.jiwigo.transverse.util.Messages;
20
21public class JTabbedPaneWithCloseIcons extends JTabbedPane implements MouseListener {
22    public JTabbedPaneWithCloseIcons() {
23        super();
24        addMouseListener(this);
25    }
26
27    public void addTab(String title, Component component) {
28        this.addTab(title, component, null);
29    }
30
31    public void addTab(String title, Component component, Icon extraIcon) {
32        super.addTab(title, new CloseTabIcon(extraIcon), component);
33        setSelectedIndex(this.getTabCount() - 1);
34    }
35
36    public void mouseClicked(MouseEvent e) {
37        Integer tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY());
38        if (tabNumber < 0)
39            return;
40        Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds();
41        if (rect.contains(e.getX(), e.getY())) {
42            //the tab is being closed
43            this.removeTabAt(tabNumber);
44        } else {
45            tabClicked(e);
46        }
47    }
48
49    public void mouseEntered(MouseEvent e) {
50    }
51
52    public void mouseExited(MouseEvent e) {
53    }
54
55    public void mousePressed(MouseEvent e) {
56    }
57
58    public void mouseReleased(MouseEvent e) {
59    }
60
61    private void tabClicked(MouseEvent e) {
62        if (e.getButton() != MouseEvent.BUTTON1 && e.getClickCount() == 1) {
63            /*
64             * thumbviewer_close=Close
65            thumbviewer_closeOthers=Close others
66            thumbviewer_closeAll=Close all
67             */
68            JPopupMenu popupMenu = new JPopupMenu();
69            JMenuItem closeBtn = new JMenuItem(Messages.getMessage("thumbviewer_close"));
70            JMenuItem closeOtherBtn = new JMenuItem(Messages.getMessage("thumbviewer_closeOthers"));
71            JMenuItem closeAll = new JMenuItem(Messages.getMessage("thumbviewer_closeAll"));
72            closeBtn.addActionListener(new ActionListener() {
73                public void actionPerformed(ActionEvent e) {
74                    SwingUtilities.invokeLater(new Runnable() {
75                        public void run() {
76                            closeSelectedTab();
77                        }
78                    });
79                }
80            });
81
82            closeOtherBtn.addActionListener(new ActionListener() {
83                public void actionPerformed(ActionEvent e) {
84                    SwingUtilities.invokeLater(new Runnable() {
85                        public void run() {
86                            closeTabs(false);
87                        }
88                    });
89                }
90            });
91
92            closeAll.addActionListener(new ActionListener() {
93                public void actionPerformed(ActionEvent e) {
94                    SwingUtilities.invokeLater(new Runnable() {
95                        public void run() {
96                            closeTabs(true);
97                        }
98                    });
99                }
100            });
101
102            popupMenu.add(closeBtn);
103            popupMenu.add(closeOtherBtn);
104            popupMenu.add(closeAll);
105            popupMenu.show(e.getComponent(), e.getX(), e.getY() - 10);
106        }
107    }
108
109    private void closeSelectedTab() {
110        // do cleanup
111        remove(getSelectedIndex()); // remove selected tab
112    }
113
114    private void closeTabs(boolean all) {
115        Component comp = getSelectedComponent();
116        ArrayList<Component> toRemove = new ArrayList<Component>();
117        for (int i = 0; i < getTabCount(); i++) {
118            if (!(getComponentAt(i).equals(comp)) && !all) {
119                toRemove.add(getComponent(i));
120            } else if (all) {
121                toRemove.add(getComponent(i));
122            }
123        }
124        for (Component compToRemove : toRemove) {
125            remove(compToRemove);
126        }
127    }
128}
129
130/**
131 * The class which generates the 'X' icon for the tabs. The constructor
132 * accepts an icon which is extra to the 'X' icon, so you can have tabs
133 * like in JBuilder. This value is null if no extra icon is required.
134 */
135class CloseTabIcon implements Icon {
136    private int x_pos;
137    private int y_pos;
138    private int width;
139    private int height;
140    private Icon fileIcon;
141
142    public CloseTabIcon(Icon fileIcon) {
143        this.fileIcon = fileIcon;
144        width = 16;
145        height = 16;
146    }
147
148    public void paintIcon(Component c, Graphics g, int x, int y) {
149        this.x_pos = x;
150        this.y_pos = y;
151
152        Color col = g.getColor();
153
154        g.setColor(Color.black);
155        int y_p = y;
156        if (fileIcon != null) {
157            fileIcon.paintIcon(c, g, x + 4, y_p + 2);
158        }
159    }
160
161    public int getIconWidth() {
162        return width;
163    }
164
165    public int getIconHeight() {
166        return height;
167    }
168
169    public Rectangle getBounds() {
170        return new Rectangle(x_pos, y_pos, width, height);
171    }
172}
Note: See TracBrowser for help on using the repository browser.