2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com
.intellij
.openapi
.vcs
.changes
.ui
;
18 import com
.intellij
.openapi
.extensions
.Extensions
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.openapi
.vcs
.VcsBundle
;
21 import com
.intellij
.openapi
.vcs
.VcsConfiguration
;
22 import com
.intellij
.openapi
.vcs
.changes
.ChangeListEditHandler
;
23 import com
.intellij
.openapi
.vcs
.changes
.ChangeListManager
;
24 import com
.intellij
.openapi
.vcs
.changes
.LocalChangeList
;
25 import com
.intellij
.ui
.DocumentAdapter
;
26 import com
.intellij
.util
.Consumer
;
27 import com
.intellij
.util
.ui
.UIUtil
;
28 import org
.jetbrains
.annotations
.Nullable
;
31 import javax
.swing
.event
.DocumentEvent
;
32 import java
.awt
.event
.InputMethodEvent
;
33 import java
.awt
.event
.InputMethodListener
;
34 import java
.awt
.event
.KeyEvent
;
35 import java
.awt
.event
.KeyListener
;
40 public abstract class EditChangelistPanel
{
41 private JTextField myNameTextField
;
42 private JTextArea myDescriptionTextArea
;
43 private JPanel myContent
;
44 private JPanel myAdditionalControlsPanel
;
45 private JCheckBox myMakeActiveCheckBox
;
48 private final ChangeListEditHandler myHandler
;
49 private Consumer
<LocalChangeList
> myConsumer
;
51 public EditChangelistPanel(@Nullable final ChangeListEditHandler handler
) {
54 myNameTextField
.addKeyListener(new KeyListener() {
55 public void keyTyped(final KeyEvent e
) {
56 onEditName(EditChangelistPanel
.this.myHandler
);
58 public void keyPressed(final KeyEvent e
) {
60 public void keyReleased(final KeyEvent e
) {
61 onEditName(EditChangelistPanel
.this.myHandler
);
64 myNameTextField
.addInputMethodListener(new InputMethodListener() {
65 public void inputMethodTextChanged(final InputMethodEvent event
) {
66 onEditName(EditChangelistPanel
.this.myHandler
);
68 public void caretPositionChanged(final InputMethodEvent event
) {
71 if (myHandler
!= null) {
72 myDescriptionTextArea
.addKeyListener(new KeyListener() {
73 public void keyTyped(final KeyEvent e
) {
75 public void keyPressed(final KeyEvent e
) {
77 public void keyReleased(final KeyEvent e
) {
78 onEditComment(EditChangelistPanel
.this.myHandler
);
81 myDescriptionTextArea
.addInputMethodListener(new InputMethodListener() {
82 public void inputMethodTextChanged(final InputMethodEvent event
) {
83 onEditComment(EditChangelistPanel
.this.myHandler
);
85 public void caretPositionChanged(final InputMethodEvent event
) {
91 public JCheckBox
getMakeActiveCheckBox() {
92 return myMakeActiveCheckBox
;
95 public void init(final Project project
, final LocalChangeList initial
) {
96 myMakeActiveCheckBox
.setSelected(VcsConfiguration
.getInstance(project
).MAKE_NEW_CHANGELIST_ACTIVE
);
97 for (EditChangelistSupport support
: Extensions
.getExtensions(EditChangelistSupport
.EP_NAME
, project
)) {
98 support
.installSearch(myNameTextField
, myDescriptionTextArea
);
99 myConsumer
= support
.addControls(myAdditionalControlsPanel
, initial
);
101 myNameTextField
.getDocument().addDocumentListener(new DocumentAdapter() {
103 protected void textChanged(DocumentEvent e
) {
104 nameChangedImpl(project
, initial
);
107 nameChangedImpl(project
, initial
);
110 protected void nameChangedImpl(final Project project
, final LocalChangeList initial
) {
111 String name
= getName();
112 if (name
== null || name
.trim().length() == 0) {
113 nameChanged("Cannot create new changelist with empty name.");
114 } else if ((initial
== null || !name
.equals(initial
.getName())) && ChangeListManager
.getInstance(project
).findChangeList(name
) != null) {
115 nameChanged(VcsBundle
.message("new.changelist.duplicate.name.error"));
121 public void changelistCreatedOrChanged(LocalChangeList list
) {
122 if (myConsumer
!= null) {
123 myConsumer
.consume(list
);
127 private void onEditComment(ChangeListEditHandler handler
) {
128 if (handler
!= null) {
129 myNameTextField
.setText(handler
.changeNameOnChangeComment(myNameTextField
.getText(), myDescriptionTextArea
.getText()));
133 private void onEditName(ChangeListEditHandler handler
) {
134 if (handler
!= null) {
135 myDescriptionTextArea
.setText(handler
.changeCommentOnChangeName(myNameTextField
.getText(), myDescriptionTextArea
.getText()));
139 public void setName(String s
) {
140 myNameTextField
.setText(s
);
143 public String
getName() {
144 return myNameTextField
.getText();
147 public void setDescription(String s
) {
148 myDescriptionTextArea
.setText(s
);
151 public String
getDescription() {
152 return myDescriptionTextArea
.getText();
155 public JComponent
getContent() {
159 public void setEnabled(boolean b
) {
160 UIUtil
.setEnabled(myContent
, b
, true);
163 public void requestFocus() {
164 myNameTextField
.requestFocus();
167 public JComponent
getPrefferedFocusedComponent() {
168 return myNameTextField
;
171 protected abstract void nameChanged(String errorMessage
);