IDEA-19061 Integrate the Rearranger-plugin into core-IDE
[idea/community.git] / platform / lang-impl / src / com / intellij / application / options / codeStyle / arrangement / match / ArrangementMatchingRulesPanel.java
1 /*
2  * Copyright 2000-2012 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.application.options.codeStyle.arrangement.match;
17
18 import com.intellij.application.options.codeStyle.arrangement.ArrangementConstants;
19 import com.intellij.application.options.codeStyle.arrangement.ArrangementNodeDisplayManager;
20 import com.intellij.application.options.codeStyle.arrangement.color.ArrangementColorsProvider;
21 import com.intellij.application.options.codeStyle.arrangement.util.TitleWithToolbar;
22 import com.intellij.ide.ui.customization.CustomizationUtil;
23 import com.intellij.openapi.actionSystem.DataProvider;
24 import com.intellij.openapi.application.ApplicationBundle;
25 import com.intellij.psi.codeStyle.arrangement.match.StdArrangementMatchRule;
26 import com.intellij.psi.codeStyle.arrangement.settings.ArrangementStandardSettingsAware;
27 import com.intellij.ui.components.JBScrollPane;
28 import com.intellij.util.ui.GridBag;
29 import org.jetbrains.annotations.NonNls;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32
33 import javax.swing.*;
34 import java.awt.*;
35 import java.util.List;
36
37 /**
38  * @author Denis Zhdanov
39  * @since 10/30/12 5:28 PM
40  */
41 public class ArrangementMatchingRulesPanel extends JPanel implements DataProvider {
42
43   @NotNull private final ArrangementMatchingRulesControl myControl;
44
45   public ArrangementMatchingRulesPanel(@NotNull ArrangementNodeDisplayManager displayManager,
46                                        @NotNull ArrangementColorsProvider colorsProvider,
47                                        @NotNull ArrangementStandardSettingsAware settingsFilter)
48   {
49     super(new GridBagLayout());
50     
51     JBScrollPane scrollPane = new JBScrollPane();
52     final JViewport viewport = scrollPane.getViewport();
53     ArrangementMatchingRulesControl.RepresentationCallback callback = new ArrangementMatchingRulesControl.RepresentationCallback() {
54       @Override
55       public void ensureVisible(@NotNull Rectangle r) {
56         Rectangle visibleRect = viewport.getViewRect();
57         if (r.y <= visibleRect.y) {
58           return;
59         }
60
61         int excessiveHeight = r.y + r.height - (visibleRect.y + visibleRect.height);
62         if (excessiveHeight <= 0) {
63           return;
64         }
65
66         int verticalShift = Math.min(r.y - visibleRect.y, excessiveHeight);
67         if (verticalShift > 0) {
68           viewport.setViewPosition(new Point(visibleRect.x, visibleRect.y + verticalShift));
69         }
70       }
71     };
72     myControl = new ArrangementMatchingRulesControl(displayManager, colorsProvider, settingsFilter, callback);
73     scrollPane.setViewportView(myControl);
74     CustomizationUtil.installPopupHandler(
75       myControl, ArrangementConstants.ACTION_GROUP_MATCHING_RULES_CONTEXT_MENU, ArrangementConstants.RULE_EDITOR_PLACE
76     );
77
78     TitleWithToolbar top = new TitleWithToolbar(
79       ApplicationBundle.message("arrangement.settings.section.match"),
80       ArrangementConstants.ACTION_GROUP_MATCHING_RULES_CONTROL_TOOLBAR,
81       ArrangementConstants.MATCHING_RULES_CONTROL_TOOLBAR_PLACE,
82       myControl
83     );
84     add(top, new GridBag().coverLine().fillCellHorizontally().weightx(1));
85     add(scrollPane, new GridBag().fillCell().weightx(1).weighty(1));
86   }
87
88   @NotNull
89   public List<StdArrangementMatchRule> getRules() {
90     return myControl.getRules();
91   }
92   
93   public void setRules(@Nullable List<StdArrangementMatchRule> rules) {
94     myControl.setRules(rules);
95   }
96
97   @Nullable
98   @Override
99   public Object getData(@NonNls String dataId) {
100     if (ArrangementConstants.MATCHING_RULES_CONTROL_KEY.is(dataId)) {
101       return myControl;
102     }
103     return null;
104   }
105 }