update copyright
[fedora-idea.git] / xml / dom-openapi / src / com / intellij / util / xml / ui / CommitablePanelUserActivityListener.java
blob82ca2b9435499a3c84b5e75cd15a8c226a6f42dc
1 /*
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.
17 package com.intellij.util.xml.ui;
19 import com.intellij.ui.UserActivityListener;
20 import com.intellij.util.Alarm;
21 import com.intellij.openapi.Disposable;
22 import com.intellij.openapi.components.ServiceManager;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.progress.ProgressManager;
25 import com.intellij.openapi.progress.ProgressIndicator;
27 /**
28 * User: Sergey.Vasiliev
30 public class CommitablePanelUserActivityListener implements UserActivityListener, Disposable {
31 private final Committable myPanel;
32 private final Project myProject;
33 private final Alarm myAlarm = new Alarm();
34 private boolean myApplying;
36 public CommitablePanelUserActivityListener(Project project) {
37 this(null, project);
40 public CommitablePanelUserActivityListener(final Committable panel, Project project) {
41 myPanel = panel;
42 myProject = project;
45 final public void stateChanged() {
46 if (myApplying) return;
47 cancel();
48 cancelAllRequests();
49 myAlarm.addRequest(new Runnable() {
50 public void run() {
51 myApplying = true;
52 cancel();
53 try {
54 applyChanges();
56 finally {
57 myApplying = false;
60 }, 717);
63 private static void cancel() {
64 final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
65 if (indicator != null) {
66 indicator.cancel();
70 protected void applyChanges() {
71 if (myPanel != null) {
72 commit(myPanel);
76 protected final void commit(final Committable panel) {
77 ServiceManager.getService(getProject(), CommittableUtil.class).commit(panel);
80 protected final Project getProject() {
81 return myProject;
84 public final boolean isWaiting() {
85 return myAlarm.getActiveRequestCount() > 0;
88 public final void cancelAllRequests() {
89 myAlarm.cancelAllRequests();
92 public void dispose() {
93 cancelAllRequests();