package jetbrains.mps.ide.ui.dialogs.modules;
/*Generated by MPS */
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import com.intellij.ui.DocumentAdapter;
import javax.swing.event.DocumentEvent;
import com.intellij.util.ui.JBUI;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.ui.InsertPathAction;
import com.intellij.ide.util.BrowseFilesListener;
import com.intellij.ui.FieldPanel;
import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.openapi.fileChooser.FileChooserFactory;
import javax.swing.JComponent;
import java.io.File;
import java.awt.Component;
import java.awt.Insets;
import java.awt.GridBagConstraints;
public abstract class AbstractModuleCreationSettings extends JPanel {
protected JTextField myModuleName;
protected JTextField myModuleLocation;
private AbstractModuleCreationSettings.ModuleCreationSettingsListener myListener;
protected boolean myLocationChangedByUser = false;
protected boolean myLocationDocListenerEnabled = true;
protected String myProjectPath;
protected AbstractModuleCreationSettings(final String projectPath, String moduleNameLabel, String moduleLocationLabel) {
super(new GridBagLayout());
myProjectPath = projectPath;
this.add(new JLabel(moduleNameLabel), 0, 0.0);
myModuleName = new JTextField();
myModuleName.setName("Name");
myModuleName.getDocument().addDocumentListener(new DocumentAdapter() {
protected void textChanged(DocumentEvent p0) {
if ((myProjectPath == null || myProjectPath.length() == 0)) {
return;
}
String path = getDefaultModulePath();
final String solutionName = getModuleName();
if (!(solutionName.equals(getModuleLocation()))) {
path += solutionName;
}
if (!(myLocationChangedByUser)) {
setModuleLocation(path);
}
}
});
this.add(myModuleName, 1, 0.0, JBUI.insetsBottom(5));
this.add(new JLabel(moduleLocationLabel), 2, 0.0);
myModuleLocation = new JTextField();
myModuleLocation.setName("Path");
myModuleLocation.getDocument().addDocumentListener(new DocumentAdapter() {
protected void textChanged(DocumentEvent p0) {
if (myLocationDocListenerEnabled) {
myLocationChangedByUser = true;
}
}
});
final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
InsertPathAction.addTo(myModuleLocation, descriptor);
BrowseFilesListener listener = new BrowseFilesListener(myModuleLocation, "Choose Module Location Folder", "", descriptor);
FieldPanel fieldPanel = new FieldPanel(myModuleLocation, null, null, listener, EmptyRunnable.getInstance());
FileChooserFactory.getInstance().installFileCompletion(fieldPanel.getTextField(), descriptor, false, null);
this.add(fieldPanel, 3, 0.0, JBUI.insetsBottom(5));
}
public String getModuleName() {
return myModuleName.getText().trim();
}
public void setModuleName(String moduleName) {
myModuleName.setText(moduleName);
fireChanged();
}
public String getModuleLocation() {
return myModuleLocation.getText().trim();
}
public void setModuleLocation(String moduleLocation) {
myLocationDocListenerEnabled = false;
myModuleLocation.setText(moduleLocation);
myLocationDocListenerEnabled = true;
fireChanged();
}
public void setProjectPath(String projectPath) {
// If path is the same - just return
if (myProjectPath != null && myProjectPath.equals(projectPath)) {
return;
}
final String oldProjectPath = myProjectPath;
myProjectPath = projectPath;
if ((oldProjectPath != null && oldProjectPath.length() > 0) && myModuleLocation.getText().contains(oldProjectPath)) {
setModuleLocation(myModuleLocation.getText().replace(oldProjectPath, myProjectPath));
} else {
setModuleLocation(getDefaultModulePath());
}
fireChanged();
}
public void setListener(AbstractModuleCreationSettings.ModuleCreationSettingsListener listener) {
myListener = listener;
}
private void fireChanged() {
if (myListener != null) {
myListener.changed();
}
}
public void reset() {
setModuleName(getDefaultModuleName());
if (myProjectPath != null) {
setModuleLocation(getDefaultModulePath() + getDefaultModuleName());
}
}
public JComponent getPreferredFocusedComponent() {
return myModuleName;
}
protected abstract String getDefaultModuleName();
protected abstract String getDefaultModulePath();
public interface ModuleCreationSettingsListener {
void changed();
}
protected String getModuleRootPath(String moduleRootLocation) {
return this.myProjectPath + File.separator + moduleRootLocation + File.separator;
}
protected void add(Component component, int row, double rowWeight) {
add(component, row, rowWeight, JBUI.emptyInsets());
}
protected void add(Component component, int row, double rowWeight, Insets insets) {
add(component, new GridBagConstraints(0, row, 1, 1, 1.0, rowWeight, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, insets, 5, 5));
}
}