Revision 562 org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.swing/org.gvsig.scripting.swing.impl/src/main/java/org/gvsig/scripting/swing/impl/composer/DefaultJScriptingComposer.java

View differences:

DefaultJScriptingComposer.java
6 6
import java.awt.Cursor;
7 7
import java.awt.Dialog;
8 8
import java.awt.Dimension;
9
import java.awt.FlowLayout;
9
import java.awt.Font;
10 10
import java.awt.event.ActionEvent;
11 11
import java.awt.event.ActionListener;
12 12
import java.awt.event.KeyEvent;
13
import java.beans.PropertyChangeEvent;
14
import java.beans.PropertyChangeListener;
15 13
import java.io.File;
16 14
import java.io.OutputStream;
17 15
import java.io.PrintStream;
......
89 87
import org.gvsig.tools.packageutils.PackageInfo;
90 88
import org.gvsig.tools.packageutils.PackageManager;
91 89
import org.gvsig.tools.packageutils.Version;
90
import org.gvsig.tools.swing.api.ActionListenerSupport;
91
import org.gvsig.tools.swing.api.ToolsSwingLocator;
92 92
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
93 93
import org.slf4j.Logger;
94 94
import org.slf4j.LoggerFactory;
......
104 104
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_L, c));
105 105
        }
106 106

  
107
        @Override
107 108
        public void actionPerformed(ActionEvent e) {
108 109

  
109 110
            RSyntaxTextArea textArea = getCurrentRSyntaxTextArea();
......
155 156
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, c));
156 157
        }
157 158

  
159
        @Override
158 160
        public void actionPerformed(ActionEvent e) {
159 161
            if (findDialog.isVisible()) {
160 162
                findDialog.setVisible(false);
......
423 425
        
424 426
        public CloseTabAction(Component component) {
425 427
            super(uimanager.getTranslation("Close_tab"));
426
            putValue(Action.SMALL_ICON, uimanager.getIcon("tabclose"));
428
            putValue(Action.SMALL_ICON, uimanager.getIcon("tabclose-inactive"));
427 429
            putValue(Action.SHORT_DESCRIPTION, uimanager.getTranslation("Close_tab"));
428 430
            this.component = component;
429 431
        }
......
608 610
    JSplitPane mainPane;
609 611
    JSplitPane editionPane;
610 612
    JTabbedPane problemsPane;
611
    JTabbedPane scriptEditors;
613
    JMyTabbedPane scriptEditors;
612 614
    JMenuBar menuBar;
613 615
    JToolBar file;
614 616
    JToolBar edit;
......
641 643
        this.uimanager = uimanager;
642 644
        this.manager = uimanager.getManager();
643 645

  
644
        unitsEditor = new LinkedHashMap<String, JEditor>();
646
        unitsEditor = new LinkedHashMap<>();
645 647

  
646 648
        initComponents();
647 649
    }
......
743 745
        launcherPane.add(launcherButtons, BorderLayout.WEST);
744 746

  
745 747
        // Panel de Edicion
746
        scriptEditors = new JTabbedPane();
748
        scriptEditors = new JMyTabbedPane();
749
        scriptEditors.addCloseTabListener(new ActionListener() {
750

  
751
            @Override
752
            public void actionPerformed(ActionEvent ae) {
753
                currentScriptClose();
754
            }
755
        });
756
        
747 757
        scriptEditors.addChangeListener(new ChangeListener() {
758
            @Override
748 759
            public void stateChanged(ChangeEvent arg0) {
749 760
                JPanel editor = (JPanel) scriptEditors.getSelectedComponent();
750 761
                if (scriptEditors.getSelectedIndex() == -1) {
......
782 793

  
783 794
        /* Si no se desea cargar datos para que arranque con ellos,
784 795
         * este if nunca será cierto */
785
        if (!unitsEditor.isEmpty()) {;
796
        if (!unitsEditor.isEmpty()) {
786 797
            Iterator<JEditor> it = unitsEditor.values().iterator();
787 798
            while (it.hasNext()) {
788 799
                JEditor editor = it.next();
......
883 894

  
884 895
    private class RowListener implements ListSelectionListener {
885 896

  
897
        @Override
886 898
        public void valueChanged(ListSelectionEvent event) {
887 899
            if (event.getValueIsAdjusting()) {
888 900
                return;
......
1049 1061
     */
1050 1062
    public void scriptEdit(ScriptingBaseScript unit) {
1051 1063
        if( ! unitsEditor.containsKey(unit.getFile().getAbsolutePath()) ) {
1052
            JEditor panel = null;
1064
            JEditor panel;
1053 1065
            if (unit instanceof ScriptingDialog) {
1054 1066
                panel = new DefaultJDialogEditor(uimanager, (ScriptingDialog) unit);
1055 1067
                if (panel instanceof DefaultJDialogEditor) {
......
1067 1079
            unitsEditor.put(unit.getFile().getAbsolutePath(), panel);
1068 1080
            String title = panel.getScript().getName();
1069 1081
            scriptEditors.addTab(title, panel);
1070
            scriptEditors.setTabComponentAt(
1071
                    scriptEditors.getTabCount()-1, 
1072
                    new TabTitleComponent(scriptEditors, title,panel)
1073
            );
1074 1082
        }
1075 1083
        int n = 0;
1076 1084
        int selected = unitsEditor.size() - 1;
......
1093 1101
        editionPane.repaint();
1094 1102
        file.repaint();
1095 1103
    }
1096

  
1097
    private class TabTitleComponent extends JPanel implements PropertyChangeListener {
1098
        private final JLabel label;
1099
        private final JButton button;
1100
        private final JTabbedPane tab;
1101
        private final JEditor editor;
1102 1104
    
1103
        public TabTitleComponent(JTabbedPane tab, String title, JEditor editor)  {
1104
            this.tab = tab;
1105
            this.editor = editor;
1106
            this.setOpaque(false);
1107
            this.label = new JLabel(title+" ");
1108
            this.label.setOpaque(false);
1109
            this.button = new JButton(new CloseTabAction(editor));
1110
            this.button.setText("");
1111
            this.button.setBorder(BorderFactory.createEmptyBorder());
1112
            this.button.setBorderPainted(false);
1113
            this.button.setFocusPainted(false);
1114
            this.button.setContentAreaFilled(false);
1115
            this.button.setCursor(new Cursor(Cursor.HAND_CURSOR));
1116
            this.setLayout(new BorderLayout());
1117
            this.add(this.label, BorderLayout.CENTER);
1118
            this.add(this.button, BorderLayout.LINE_END);
1119
            this.tab.addPropertyChangeListener(this);
1120
        }
1121

  
1122
        @Override
1123
        public void propertyChange(PropertyChangeEvent pce) {
1124
            if( "indexForTitle".equalsIgnoreCase(pce.getPropertyName()) ) {
1125
                int index = (int) pce.getNewValue();
1126
                int myindex = tab.indexOfComponent(this.editor);
1127
                if( myindex == index ) {
1128
                    this.label.setText(this.tab.getTitleAt(index) + " ");
1129
                }
1130
            }
1131
        }
1132
    }
1133
    
1134
    
1135 1105
    /**
1136 1106
     * Ventana para creación de nuevos ScriptingBaseScripts
1137 1107
     *
......
1464 1434
            int pestanaIndex = scriptEditors.getSelectedIndex();
1465 1435
            JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(pestanaIndex);
1466 1436
            pestanaEditor.getJTextComponent().cut();
1467
            JTabbedPane tabs = (JTabbedPane) pestanaEditor.getParent();
1468

  
1469
            String title = tabs.getTitleAt(pestanaIndex);
1470
            if (title.length() > 0 && !title.substring(0, 1).equals("*")) {
1471
                tabs.setTitleAt(tabs.getSelectedIndex(), "*" + title);
1437
            if( !scriptEditors.isTabChanged(pestanaIndex) ) {
1438
                scriptEditors.setTabChanged(pestanaIndex,true);
1472 1439
                pestanaEditor.getScript().setSaved(false);
1473 1440
            }
1474 1441
        }
......
1479 1446
            int pestanaIndex = scriptEditors.getSelectedIndex();
1480 1447
            JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(pestanaIndex);
1481 1448
            pestanaEditor.getJTextComponent().paste();
1482
            JTabbedPane tabs = (JTabbedPane) pestanaEditor.getParent();
1483

  
1484
            String title = tabs.getTitleAt(pestanaIndex);
1485
            if (title.length() > 0 && !title.substring(0, 1).equals("*")) {
1486
                tabs.setTitleAt(tabs.getSelectedIndex(), "*" + title);
1449
            if( !scriptEditors.isTabChanged(pestanaIndex) ) {
1450
                scriptEditors.setTabChanged(pestanaIndex,true);
1487 1451
                pestanaEditor.getScript().setSaved(false);
1488 1452
            }
1489 1453
        }
......
1506 1470
            int pestanaIndex = scriptEditors.getSelectedIndex();
1507 1471
            JEditor pestanaEditor = (JEditor) scriptEditors.getComponentAt(pestanaIndex);
1508 1472
            pestanaEditor.save();
1509
            scriptEditors.setTitleAt(pestanaIndex, pestanaEditor.getScript().getName());
1473
            scriptEditors.setTabChanged(pestanaIndex,false);
1474
            pestanaEditor.getScript().setSaved(true);
1510 1475
        }
1511 1476
    }
1512 1477

  
......
1798 1763
        return textArea.getSelectedText();
1799 1764
    }
1800 1765

  
1766

  
1767
    private class TabTitleComponent extends JPanel {
1768
        private final JLabel label;
1769
        private final JButton button;
1770
        private boolean changed = false;
1771
        private final Font fontNormal;
1772
        private final Font fontBold;
1773
        
1774
        public TabTitleComponent(String title, final ActionListener onclose)  {
1775
            this.setOpaque(true);
1776
            this.label = new JLabel(title+" ");
1777
            this.label.setOpaque(true);
1778
            this.label.setBackground(UIManager.getColor("TabbedPane.tabAreaBackground"));
1779
            this.label.setVerticalTextPosition(SwingConstants.CENTER);
1780
            this.fontNormal = this.label.getFont();
1781
            this.fontBold = new Font(this.fontNormal.getFontName(), Font.BOLD, this.fontNormal.getSize());
1782
            
1783
            this.button = new JButton();
1784
            this.button.setText("");
1785
            this.button.setToolTipText(uimanager.getTranslation("Close_tab"));
1786
            this.button.setBackground(UIManager.getColor("TabbedPane.tabAreaBackground"));
1787
            this.button.setBorder(BorderFactory.createEmptyBorder());
1788
            this.button.setBorderPainted(false);
1789
            this.button.setFocusPainted(false);
1790
            this.button.setContentAreaFilled(false);
1791
            this.button.setCursor(new Cursor(Cursor.HAND_CURSOR));
1792
            this.button.setRolloverEnabled(true);
1793
            this.button.setIcon(uimanager.getIcon("tabclose-inactive"));
1794
            this.button.setRolloverIcon(uimanager.getIcon("tabclose-active"));
1795
            this.button.addActionListener(new ActionListener() {
1796
                @Override
1797
                public void actionPerformed(ActionEvent ae) {
1798
                    ae.setSource(TabTitleComponent.this);
1799
                    onclose.actionPerformed(ae);
1800
                }
1801
            });
1802
            this.setLayout(new BorderLayout());
1803
            this.add(this.label, BorderLayout.CENTER);
1804
            this.add(this.button, BorderLayout.EAST);
1805
        }
1806
        
1807
        public void setTitle(String title) {
1808
            this.label.setText(title);
1809
        }
1810
        
1811
        public String getTitle() {
1812
            return this.label.getText();
1813
        }
1814
        
1815
        public void setChanged(boolean changed) {
1816
            if( changed ) {
1817
                this.label.setFont(this.fontBold);
1818
            } else {
1819
                this.label.setFont(this.fontNormal);
1820
            }
1821
            this.changed = changed;
1822
        }
1823
        
1824
        public boolean isChanged() {
1825
            return this.changed;
1826
        }
1827
    }
1828
    
1829
    public class JMyTabbedPane extends JTabbedPane {
1830
        
1831
        private ActionListenerSupport closeTabListeners;
1832
        
1833
        public JMyTabbedPane() {
1834
            super();
1835
            this.closeTabListeners = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
1836
        }
1837

  
1838
        @Override
1839
        public String getTitleAt(int index) {
1840
            TabTitleComponent title =(TabTitleComponent) this.getTabComponentAt(index);
1841
            if( title == null ) {
1842
                return null;
1843
            }
1844
            return title.getTitle();
1845
        }
1846

  
1847
        @Override
1848
        public void setTitleAt(int index, String title) {
1849
            TabTitleComponent titleComponent =(TabTitleComponent) this.getTabComponentAt(index);
1850
            titleComponent.setTitle(title);
1851
        }
1852

  
1853
        @Override
1854
        public void addTab(String title, Component component) {
1855
            super.addTab(null, component); 
1856
            int index = getTabCount()-1;
1857
            TabTitleComponent tabcomponent = new TabTitleComponent(title, new ActionListener() {
1858
                
1859
                @Override
1860
                public void actionPerformed(ActionEvent ae) {
1861
                    TabTitleComponent tabTitleComponent = (TabTitleComponent) ae.getSource();
1862
                    int index = indexOfTabComponent(tabTitleComponent);
1863
                    setSelectedIndex(index);
1864
                    closeTabListeners.fireActionEvent(ae);
1865
                }
1866
            });
1867
            super.setTabComponentAt(index, tabcomponent);
1868
        }
1869

  
1870
        public void addCloseTabListener(ActionListener remove) {
1871
            this.closeTabListeners.addActionListener(remove);
1872
        }
1873
        
1874
        public void removeCloseTabListener(ActionListener remove) {
1875
            this.closeTabListeners.removeActionListener(remove);
1876
        }
1877
        
1878
        public ActionListener[] getCloseTabListeners() {
1879
            return this.closeTabListeners.getActionListeners();
1880
        }
1881
        
1882
        public boolean isTabChanged(int index) {
1883
            TabTitleComponent comp = (TabTitleComponent) this.getTabComponentAt(index);
1884
            if( comp == null ) {
1885
                return false;
1886
            }
1887
            return comp.isChanged();
1888
        }
1889
        
1890
        public void setTabChanged(int index, boolean changed) {
1891
            TabTitleComponent comp = (TabTitleComponent) this.getTabComponentAt(index);
1892
            if( comp != null ) {
1893
                comp.setChanged(changed);
1894
            }
1895
        }
1896
    }
1897
    
1801 1898
}

Also available in: Unified diff