summaryrefslogtreecommitdiffstats
path: root/src/cppcheckplus/outlookpanel/MyOutlookPanel.java
blob: afa7392cde8f03fa356586699a6f4f252ac184c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package cppcheckplus.outlookpanel;

import cppcheckplus.control.MyContorlUtil;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionListener;
import layout.TableLayout;

public class MyOutlookPanel extends JPanel {
    //Header
    private MyOutlookHeader header;
    //容器Panel
    private JPanel barPane;
    ;
    //右边的分割滑动线条
    private JPanel split;
    private int splitWidth;
    private Color splitColor;
    private JPanel additionalPane;
    private Hashtable componentLayoutRows;
    private JPanel centerPane;
    private TableLayout barPaneLayout;
    private MyOutlookSplitListener splitListener;
    private ListSelectionListener listSelectionListener;

    /**
     * 构造MyOutlookPanel
     *
     * @param headerListener        Header的监听器
     * @param listSelectionListener ListItemSelection的监听器
     */
    public MyOutlookPanel(ActionListener headerListener,
                          ListSelectionListener listSelectionListener) {
        super();
        //注册Header事件
        this.header = new MyOutlookHeader(headerListener) {
            public void setShrink(boolean shrinked) {
                super.setShrink(shrinked);
                shrinkChanged(shrinked);
            }

        };
        this.listSelectionListener = listSelectionListener;
        barPaneLayout = new TableLayout();
        barPane = new JPanel(barPaneLayout);

        additionalPane = new JPanel(new BorderLayout());
        componentLayoutRows = new Hashtable();
        centerPane = new JPanel(new BorderLayout());

        //分割条
        split = new JPanel();
        splitWidth = 1;
        splitColor = new Color(166, 172, 174);
        splitListener = new MyOutlookSplitListener(header);

        init();
    }

    private void init() {
        split.setPreferredSize(new Dimension(splitWidth, 0));
        split.setOpaque(true);
        split.setBackground(splitColor);
        split.setCursor(Cursor.getPredefinedCursor(10));
        split.addMouseListener(splitListener);
        split.addMouseMotionListener(splitListener);
        additionalPane.setBackground(MyContorlUtil.OUTLOOK_CONTAINER_COLOR);
        additionalPane.setPreferredSize(new Dimension(0, 0));
        additionalPane.setBorder(new Border() {

            public void paintBorder(Component c, Graphics g, int x, int y,
                                    int width, int height) {
                g.setColor(MyContorlUtil.OUTLOOK_SPLIT_COLOR);
                g.drawLine(0, 0, width, 0);
            }

            public Insets getBorderInsets(Component c) {
                return new Insets(1, 0, 0, 0);
            }

            public boolean isBorderOpaque() {
                return true;
            }

        });

        centerPane.add(barPane, "North");
        centerPane.add(additionalPane, "Center");
        barPaneLayout.insertColumn(0, -1D);
        setLayout(new BorderLayout());
        add(header, "North");
        add(centerPane, "Center");
        add(split, "East");
    }

    public MyOutlookBar addBar(String title, Icon icon, Icon selectedIcon,
                               MyOutlookPanelListItem[] listItems) {
        MyOutlookBar bar = new MyOutlookBar(this);
        MyOutlookListPanel listPanel = getListPanel(listItems, bar);
        //注册JList事件
        listPanel.getOutlookList()
            .addListSelectionListener(listSelectionListener);
        bar.setOutlookListPanel(listPanel);
        bar.setSelected(false);
        bar.setTitle(title);
        bar.setIcon(icon);
        bar.setSelectedIcon(selectedIcon);
        int rowCount = barPaneLayout.getRow().length;
        barPaneLayout.insertRow(rowCount, -2D);
        barPane.add(bar,
            (new StringBuilder()).append("0,").append(rowCount).toString());
        componentLayoutRows.put(bar, Integer.valueOf(rowCount));
        rowCount++;
        barPaneLayout.insertRow(rowCount, -3D);
        //加载列表
        barPane.add(listPanel,
            (new StringBuilder()).append("0,").append(rowCount).toString());
        componentLayoutRows.put(bar.getContentComponent(),
            Integer.valueOf(rowCount));
        return bar;
    }

    private MyOutlookListPanel getListPanel(MyOutlookPanelListItem[] listItems,
                                            MyOutlookBar bar) {
        MyOutlookList list = new MyOutlookList(listItems, bar);
        MyOutlookListPanel listPanel = new MyOutlookListPanel(list);
        return listPanel;
    }

    public void updateLayoutConstraint(Component component, boolean selected) {
        int rowIndex =
            ((Integer) componentLayoutRows.get(component)).intValue();
        double constraint = -1D;
        if (!selected) {
            constraint = -3D;
        }
        barPaneLayout.setRow(rowIndex, constraint);
    }

    public JComponent getAdditionalPane() {
        return additionalPane;
    }

    public void setAdditionalPaneVisible(boolean visible) {
        centerPane.remove(barPane);
        centerPane.remove(additionalPane);
        if (visible) {
            centerPane.add(barPane, "North");
            centerPane.add(additionalPane, "Center");
        } else {
            centerPane.add(barPane, "Center");
        }
    }

    public void closeAllBars() {
        for (int i = 0; i < barPane.getComponentCount(); i++) {
            Component c = barPane.getComponent(i);
            if (!(c instanceof MyOutlookBar)) {
                continue;
            }
            MyOutlookBar bar = (MyOutlookBar) c;
            if (bar.isSelected()) {
                bar.setSelected(false);
            }
        }
    }

    public MyOutlookBar getSelectedBar() {
        for (int i = 0; i < barPane.getComponentCount(); i++) {
            Component c = barPane.getComponent(i);
            if (!(c instanceof MyOutlookBar)) {
                continue;
            }
            MyOutlookBar bar = (MyOutlookBar) c;
            if (bar.isSelected()) {
                return bar;
            }
        }

        return null;
    }

    public void setShrink(boolean shrinked) {
        header.setShrink(shrinked);
    }

    public boolean isShrinked() {
        return header.isShrinked();
    }

    private void shrinkChanged(boolean shrinked) {
        if (shrinked) {
            split.setCursor(Cursor.getDefaultCursor());
        } else {
            split.setCursor(Cursor.getPredefinedCursor(10));
        }
        for (int i = 0; i < barPane.getComponentCount(); i++) {
            Component c = barPane.getComponent(i);
            if (c instanceof MyOutlookBar) {
                MyOutlookBar bar = (MyOutlookBar) c;
                bar.headerShrinkChanged(shrinked);


                //XOutlookList list = bar.getList();

                //list.firePropertyChange("layoutOrientation", true, false);
            }
        }
    }
}