You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditRepositoryDialog.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * Copyright 2011 gitblit.com.
  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.gitblit.client;
  17. import java.awt.BorderLayout;
  18. import java.awt.Component;
  19. import java.awt.Dimension;
  20. import java.awt.FlowLayout;
  21. import java.awt.Font;
  22. import java.awt.GridLayout;
  23. import java.awt.Insets;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.ItemEvent;
  27. import java.awt.event.ItemListener;
  28. import java.awt.event.KeyEvent;
  29. import java.text.MessageFormat;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. import java.util.HashSet;
  33. import java.util.LinkedHashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.Set;
  37. import javax.swing.BoxLayout;
  38. import javax.swing.ButtonGroup;
  39. import javax.swing.DefaultListCellRenderer;
  40. import javax.swing.ImageIcon;
  41. import javax.swing.JButton;
  42. import javax.swing.JCheckBox;
  43. import javax.swing.JComboBox;
  44. import javax.swing.JComponent;
  45. import javax.swing.JDialog;
  46. import javax.swing.JLabel;
  47. import javax.swing.JList;
  48. import javax.swing.JOptionPane;
  49. import javax.swing.JPanel;
  50. import javax.swing.JRadioButton;
  51. import javax.swing.JRootPane;
  52. import javax.swing.JScrollPane;
  53. import javax.swing.JTabbedPane;
  54. import javax.swing.JTextField;
  55. import javax.swing.KeyStroke;
  56. import javax.swing.ListCellRenderer;
  57. import javax.swing.ScrollPaneConstants;
  58. import com.gitblit.Constants.AccessRestrictionType;
  59. import com.gitblit.Constants.AuthorizationControl;
  60. import com.gitblit.Constants.FederationStrategy;
  61. import com.gitblit.Constants.RegistrantType;
  62. import com.gitblit.models.RegistrantAccessPermission;
  63. import com.gitblit.models.RepositoryModel;
  64. import com.gitblit.utils.ArrayUtils;
  65. import com.gitblit.utils.StringUtils;
  66. /**
  67. * Dialog to create/edit a repository.
  68. *
  69. * @author James Moger
  70. */
  71. public class EditRepositoryDialog extends JDialog {
  72. private static final long serialVersionUID = 1L;
  73. private final String repositoryName;
  74. private final RepositoryModel repository;
  75. private boolean isCreate;
  76. private boolean canceled = true;
  77. private JTextField nameField;
  78. private JTextField descriptionField;
  79. private JCheckBox useDocs;
  80. private JCheckBox useIncrementalPushTags;
  81. private JCheckBox showRemoteBranches;
  82. private JCheckBox skipSizeCalculation;
  83. private JCheckBox skipSummaryMetrics;
  84. private JCheckBox isFrozen;
  85. private JTextField mailingListsField;
  86. private JComboBox accessRestriction;
  87. private JRadioButton allowAuthenticated;
  88. private JRadioButton allowNamed;
  89. private JCheckBox allowForks;
  90. private JCheckBox verifyCommitter;
  91. private JComboBox federationStrategy;
  92. private JPalette<String> ownersPalette;
  93. private JComboBox headRefField;
  94. private JComboBox gcPeriod;
  95. private JTextField gcThreshold;
  96. private JComboBox maxActivityCommits;
  97. private RegistrantPermissionsPanel usersPalette;
  98. private JPalette<String> setsPalette;
  99. private RegistrantPermissionsPanel teamsPalette;
  100. private JPalette<String> indexedBranchesPalette;
  101. private JPalette<String> preReceivePalette;
  102. private JLabel preReceiveInherited;
  103. private JPalette<String> postReceivePalette;
  104. private JLabel postReceiveInherited;
  105. private Set<String> repositoryNames;
  106. private JPanel customFieldsPanel;
  107. private List<JTextField> customTextfields;
  108. public EditRepositoryDialog(int protocolVersion) {
  109. this(protocolVersion, new RepositoryModel());
  110. this.isCreate = true;
  111. setTitle(Translation.get("gb.newRepository"));
  112. }
  113. public EditRepositoryDialog(int protocolVersion, RepositoryModel aRepository) {
  114. super();
  115. this.repositoryName = aRepository.name;
  116. this.repository = new RepositoryModel();
  117. this.repositoryNames = new HashSet<String>();
  118. this.isCreate = false;
  119. initialize(protocolVersion, aRepository);
  120. setModal(true);
  121. setResizable(false);
  122. setTitle(Translation.get("gb.edit") + ": " + aRepository.name);
  123. setIconImage(new ImageIcon(getClass()
  124. .getResource("/gitblt-favicon.png")).getImage());
  125. }
  126. @Override
  127. protected JRootPane createRootPane() {
  128. KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
  129. JRootPane rootPane = new JRootPane();
  130. rootPane.registerKeyboardAction(new ActionListener() {
  131. @Override
  132. public void actionPerformed(ActionEvent actionEvent) {
  133. setVisible(false);
  134. }
  135. }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
  136. return rootPane;
  137. }
  138. private void initialize(int protocolVersion, RepositoryModel anRepository) {
  139. nameField = new JTextField(anRepository.name == null ? ""
  140. : anRepository.name, 35);
  141. descriptionField = new JTextField(anRepository.description == null ? ""
  142. : anRepository.description, 35);
  143. JTextField originField = new JTextField(
  144. anRepository.origin == null ? "" : anRepository.origin, 40);
  145. originField.setEditable(false);
  146. if (ArrayUtils.isEmpty(anRepository.availableRefs)) {
  147. headRefField = new JComboBox();
  148. headRefField.setEnabled(false);
  149. } else {
  150. headRefField = new JComboBox(
  151. anRepository.availableRefs.toArray());
  152. headRefField.setSelectedItem(anRepository.HEAD);
  153. }
  154. Integer [] gcPeriods = { 1, 2, 3, 4, 5, 7, 10, 14 };
  155. gcPeriod = new JComboBox(gcPeriods);
  156. gcPeriod.setSelectedItem(anRepository.gcPeriod);
  157. gcThreshold = new JTextField(8);
  158. gcThreshold.setText(anRepository.gcThreshold);
  159. ownersPalette = new JPalette<String>(true);
  160. useDocs = new JCheckBox(Translation.get("gb.useDocsDescription"),
  161. anRepository.useDocs);
  162. useIncrementalPushTags = new JCheckBox(Translation.get("gb.useIncrementalPushTagsDescription"),
  163. anRepository.useIncrementalPushTags);
  164. showRemoteBranches = new JCheckBox(
  165. Translation.get("gb.showRemoteBranchesDescription"),
  166. anRepository.showRemoteBranches);
  167. skipSizeCalculation = new JCheckBox(
  168. Translation.get("gb.skipSizeCalculationDescription"),
  169. anRepository.skipSizeCalculation);
  170. skipSummaryMetrics = new JCheckBox(
  171. Translation.get("gb.skipSummaryMetricsDescription"),
  172. anRepository.skipSummaryMetrics);
  173. isFrozen = new JCheckBox(Translation.get("gb.isFrozenDescription"),
  174. anRepository.isFrozen);
  175. maxActivityCommits = new JComboBox(new Integer [] { -1, 0, 25, 50, 75, 100, 150, 250, 500 });
  176. maxActivityCommits.setSelectedItem(anRepository.maxActivityCommits);
  177. mailingListsField = new JTextField(
  178. ArrayUtils.isEmpty(anRepository.mailingLists) ? ""
  179. : StringUtils.flattenStrings(anRepository.mailingLists,
  180. " "), 50);
  181. accessRestriction = new JComboBox(AccessRestrictionType.values());
  182. accessRestriction.setRenderer(new AccessRestrictionRenderer());
  183. accessRestriction.setSelectedItem(anRepository.accessRestriction);
  184. accessRestriction.addItemListener(new ItemListener() {
  185. @Override
  186. public void itemStateChanged(ItemEvent e) {
  187. if (e.getStateChange() == ItemEvent.SELECTED) {
  188. AccessRestrictionType art = (AccessRestrictionType) accessRestriction.getSelectedItem();
  189. EditRepositoryDialog.this.setupAccessPermissions(art);
  190. }
  191. }
  192. });
  193. boolean authenticated = anRepository.authorizationControl != null
  194. && AuthorizationControl.AUTHENTICATED.equals(anRepository.authorizationControl);
  195. allowAuthenticated = new JRadioButton(Translation.get("gb.allowAuthenticatedDescription"));
  196. allowAuthenticated.setSelected(authenticated);
  197. allowAuthenticated.addItemListener(new ItemListener() {
  198. @Override
  199. public void itemStateChanged(ItemEvent e) {
  200. if (e.getStateChange() == ItemEvent.SELECTED) {
  201. usersPalette.setEnabled(false);
  202. teamsPalette.setEnabled(false);
  203. }
  204. }
  205. });
  206. allowNamed = new JRadioButton(Translation.get("gb.allowNamedDescription"));
  207. allowNamed.setSelected(!authenticated);
  208. allowNamed.addItemListener(new ItemListener() {
  209. @Override
  210. public void itemStateChanged(ItemEvent e) {
  211. if (e.getStateChange() == ItemEvent.SELECTED) {
  212. usersPalette.setEnabled(true);
  213. teamsPalette.setEnabled(true);
  214. }
  215. }
  216. });
  217. ButtonGroup group = new ButtonGroup();
  218. group.add(allowAuthenticated);
  219. group.add(allowNamed);
  220. JPanel authorizationPanel = new JPanel(new GridLayout(0, 1));
  221. authorizationPanel.add(allowAuthenticated);
  222. authorizationPanel.add(allowNamed);
  223. allowForks = new JCheckBox(Translation.get("gb.allowForksDescription"), anRepository.allowForks);
  224. verifyCommitter = new JCheckBox(Translation.get("gb.verifyCommitterDescription"), anRepository.verifyCommitter);
  225. // federation strategies - remove ORIGIN choice if this repository has
  226. // no origin.
  227. List<FederationStrategy> federationStrategies = new ArrayList<FederationStrategy>(
  228. Arrays.asList(FederationStrategy.values()));
  229. if (StringUtils.isEmpty(anRepository.origin)) {
  230. federationStrategies.remove(FederationStrategy.FEDERATE_ORIGIN);
  231. }
  232. federationStrategy = new JComboBox(federationStrategies.toArray());
  233. federationStrategy.setRenderer(new FederationStrategyRenderer());
  234. federationStrategy.setSelectedItem(anRepository.federationStrategy);
  235. JPanel fieldsPanel = new JPanel(new GridLayout(0, 1));
  236. fieldsPanel.add(newFieldPanel(Translation.get("gb.name"), nameField));
  237. fieldsPanel.add(newFieldPanel(Translation.get("gb.description"),
  238. descriptionField));
  239. fieldsPanel
  240. .add(newFieldPanel(Translation.get("gb.origin"), originField));
  241. fieldsPanel.add(newFieldPanel(Translation.get("gb.headRef"), headRefField));
  242. fieldsPanel.add(newFieldPanel(Translation.get("gb.gcPeriod"), gcPeriod));
  243. fieldsPanel.add(newFieldPanel(Translation.get("gb.gcThreshold"), gcThreshold));
  244. fieldsPanel
  245. .add(newFieldPanel(Translation.get("gb.enableDocs"), useDocs));
  246. fieldsPanel
  247. .add(newFieldPanel(Translation.get("gb.enableIncrementalPushTags"), useIncrementalPushTags));
  248. fieldsPanel.add(newFieldPanel(Translation.get("gb.showRemoteBranches"),
  249. showRemoteBranches));
  250. fieldsPanel
  251. .add(newFieldPanel(Translation.get("gb.skipSizeCalculation"),
  252. skipSizeCalculation));
  253. fieldsPanel.add(newFieldPanel(Translation.get("gb.skipSummaryMetrics"),
  254. skipSummaryMetrics));
  255. fieldsPanel.add(newFieldPanel(Translation.get("gb.maxActivityCommits"),
  256. maxActivityCommits));
  257. fieldsPanel.add(newFieldPanel(Translation.get("gb.mailingLists"),
  258. mailingListsField));
  259. JPanel clonePushPanel = new JPanel(new GridLayout(0, 1));
  260. clonePushPanel
  261. .add(newFieldPanel(Translation.get("gb.isFrozen"), isFrozen));
  262. clonePushPanel
  263. .add(newFieldPanel(Translation.get("gb.allowForks"), allowForks));
  264. clonePushPanel
  265. .add(newFieldPanel(Translation.get("gb.verifyCommitter"), verifyCommitter));
  266. usersPalette = new RegistrantPermissionsPanel(RegistrantType.USER);
  267. JPanel northFieldsPanel = new JPanel(new BorderLayout(0, 5));
  268. northFieldsPanel.add(newFieldPanel(Translation.get("gb.owners"), ownersPalette), BorderLayout.NORTH);
  269. northFieldsPanel.add(newFieldPanel(Translation.get("gb.accessRestriction"),
  270. accessRestriction), BorderLayout.CENTER);
  271. JPanel northAccessPanel = new JPanel(new BorderLayout(5, 5));
  272. northAccessPanel.add(northFieldsPanel, BorderLayout.NORTH);
  273. northAccessPanel.add(newFieldPanel(Translation.get("gb.authorizationControl"),
  274. authorizationPanel), BorderLayout.CENTER);
  275. northAccessPanel.add(clonePushPanel, BorderLayout.SOUTH);
  276. JPanel accessPanel = new JPanel(new BorderLayout(5, 5));
  277. accessPanel.add(northAccessPanel, BorderLayout.NORTH);
  278. accessPanel.add(newFieldPanel(Translation.get("gb.userPermissions"),
  279. usersPalette), BorderLayout.CENTER);
  280. teamsPalette = new RegistrantPermissionsPanel(RegistrantType.TEAM);
  281. JPanel teamsPanel = new JPanel(new BorderLayout(5, 5));
  282. teamsPanel.add(
  283. newFieldPanel(Translation.get("gb.teamPermissions"),
  284. teamsPalette), BorderLayout.CENTER);
  285. setsPalette = new JPalette<String>();
  286. JPanel federationPanel = new JPanel(new BorderLayout(5, 5));
  287. federationPanel.add(
  288. newFieldPanel(Translation.get("gb.federationStrategy"),
  289. federationStrategy), BorderLayout.NORTH);
  290. federationPanel
  291. .add(newFieldPanel(Translation.get("gb.federationSets"),
  292. setsPalette), BorderLayout.CENTER);
  293. indexedBranchesPalette = new JPalette<String>();
  294. JPanel indexedBranchesPanel = new JPanel(new BorderLayout(5, 5));
  295. indexedBranchesPanel
  296. .add(newFieldPanel(Translation.get("gb.indexedBranches"),
  297. indexedBranchesPalette), BorderLayout.CENTER);
  298. preReceivePalette = new JPalette<String>(true);
  299. preReceiveInherited = new JLabel();
  300. JPanel preReceivePanel = new JPanel(new BorderLayout(5, 5));
  301. preReceivePanel.add(preReceivePalette, BorderLayout.CENTER);
  302. preReceivePanel.add(preReceiveInherited, BorderLayout.WEST);
  303. postReceivePalette = new JPalette<String>(true);
  304. postReceiveInherited = new JLabel();
  305. JPanel postReceivePanel = new JPanel(new BorderLayout(5, 5));
  306. postReceivePanel.add(postReceivePalette, BorderLayout.CENTER);
  307. postReceivePanel.add(postReceiveInherited, BorderLayout.WEST);
  308. customFieldsPanel = new JPanel();
  309. customFieldsPanel.setLayout(new BoxLayout(customFieldsPanel, BoxLayout.Y_AXIS));
  310. JScrollPane customFieldsScrollPane = new JScrollPane(customFieldsPanel);
  311. customFieldsScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  312. customFieldsScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
  313. JTabbedPane panel = new JTabbedPane(JTabbedPane.TOP);
  314. panel.addTab(Translation.get("gb.general"), fieldsPanel);
  315. panel.addTab(Translation.get("gb.accessRestriction"), accessPanel);
  316. if (protocolVersion >= 2) {
  317. panel.addTab(Translation.get("gb.teams"), teamsPanel);
  318. }
  319. panel.addTab(Translation.get("gb.federation"), federationPanel);
  320. if (protocolVersion >= 3) {
  321. panel.addTab(Translation.get("gb.indexedBranches"), indexedBranchesPanel);
  322. }
  323. panel.addTab(Translation.get("gb.preReceiveScripts"), preReceivePanel);
  324. panel.addTab(Translation.get("gb.postReceiveScripts"), postReceivePanel);
  325. panel.addTab(Translation.get("gb.customFields"), customFieldsScrollPane);
  326. setupAccessPermissions(anRepository.accessRestriction);
  327. JButton createButton = new JButton(Translation.get("gb.save"));
  328. createButton.addActionListener(new ActionListener() {
  329. @Override
  330. public void actionPerformed(ActionEvent event) {
  331. if (validateFields()) {
  332. canceled = false;
  333. setVisible(false);
  334. }
  335. }
  336. });
  337. JButton cancelButton = new JButton(Translation.get("gb.cancel"));
  338. cancelButton.addActionListener(new ActionListener() {
  339. @Override
  340. public void actionPerformed(ActionEvent event) {
  341. canceled = true;
  342. setVisible(false);
  343. }
  344. });
  345. JPanel controls = new JPanel();
  346. controls.add(cancelButton);
  347. controls.add(createButton);
  348. final Insets _insets = new Insets(5, 5, 5, 5);
  349. JPanel centerPanel = new JPanel(new BorderLayout(5, 5)) {
  350. private static final long serialVersionUID = 1L;
  351. @Override
  352. public Insets getInsets() {
  353. return _insets;
  354. }
  355. };
  356. centerPanel.add(panel, BorderLayout.CENTER);
  357. centerPanel.add(controls, BorderLayout.SOUTH);
  358. getContentPane().setLayout(new BorderLayout(5, 5));
  359. getContentPane().add(centerPanel, BorderLayout.CENTER);
  360. pack();
  361. nameField.requestFocus();
  362. }
  363. private JPanel newFieldPanel(String label, JComponent comp) {
  364. return newFieldPanel(label, 150, comp);
  365. }
  366. private JPanel newFieldPanel(String label, int labelSize, JComponent comp) {
  367. JLabel fieldLabel = new JLabel(label);
  368. fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD));
  369. fieldLabel.setPreferredSize(new Dimension(labelSize, 20));
  370. JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
  371. panel.add(fieldLabel);
  372. panel.add(comp);
  373. return panel;
  374. }
  375. private void setupAccessPermissions(AccessRestrictionType art) {
  376. if (AccessRestrictionType.NONE.equals(art)) {
  377. usersPalette.setEnabled(false);
  378. teamsPalette.setEnabled(false);
  379. allowAuthenticated.setEnabled(false);
  380. allowNamed.setEnabled(false);
  381. verifyCommitter.setEnabled(false);
  382. } else {
  383. allowAuthenticated.setEnabled(true);
  384. allowNamed.setEnabled(true);
  385. verifyCommitter.setEnabled(true);
  386. if (allowNamed.isSelected()) {
  387. usersPalette.setEnabled(true);
  388. teamsPalette.setEnabled(true);
  389. }
  390. }
  391. }
  392. private boolean validateFields() {
  393. String rname = nameField.getText();
  394. if (StringUtils.isEmpty(rname)) {
  395. error("Please enter a repository name!");
  396. return false;
  397. }
  398. // automatically convert backslashes to forward slashes
  399. rname = rname.replace('\\', '/');
  400. // Automatically replace // with /
  401. rname = rname.replace("//", "/");
  402. // prohibit folder paths
  403. if (rname.startsWith("/")) {
  404. error("Leading root folder references (/) are prohibited.");
  405. return false;
  406. }
  407. if (rname.startsWith("../")) {
  408. error("Relative folder references (../) are prohibited.");
  409. return false;
  410. }
  411. if (rname.contains("/../")) {
  412. error("Relative folder references (../) are prohibited.");
  413. return false;
  414. }
  415. if (rname.endsWith("/")) {
  416. rname = rname.substring(0, rname.length() - 1);
  417. }
  418. // confirm valid characters in repository name
  419. Character c = StringUtils.findInvalidCharacter(rname);
  420. if (c != null) {
  421. error(MessageFormat.format(
  422. "Illegal character ''{0}'' in repository name!", c));
  423. return false;
  424. }
  425. // verify repository name uniqueness on create
  426. if (isCreate) {
  427. // force repo names to lowercase
  428. // this means that repository name checking for rpc creation
  429. // is case-insensitive, regardless of the Gitblit server's
  430. // filesystem
  431. if (repositoryNames.contains(rname.toLowerCase())) {
  432. error(MessageFormat
  433. .format("Can not create repository ''{0}'' because it already exists.",
  434. rname));
  435. return false;
  436. }
  437. } else {
  438. // check rename collision
  439. if (!repositoryName.equalsIgnoreCase(rname)) {
  440. if (repositoryNames.contains(rname.toLowerCase())) {
  441. error(MessageFormat
  442. .format("Failed to rename ''{0}'' because ''{1}'' already exists.",
  443. repositoryName, rname));
  444. return false;
  445. }
  446. }
  447. }
  448. if (accessRestriction.getSelectedItem() == null) {
  449. error("Please select access restriction!");
  450. return false;
  451. }
  452. if (federationStrategy.getSelectedItem() == null) {
  453. error("Please select federation strategy!");
  454. return false;
  455. }
  456. repository.name = rname;
  457. repository.description = descriptionField.getText();
  458. repository.owners.clear();
  459. repository.owners.addAll(ownersPalette.getSelections());
  460. repository.HEAD = headRefField.getSelectedItem() == null ? null
  461. : headRefField.getSelectedItem().toString();
  462. repository.gcPeriod = (Integer) gcPeriod.getSelectedItem();
  463. repository.gcThreshold = gcThreshold.getText();
  464. repository.useDocs = useDocs.isSelected();
  465. repository.useIncrementalPushTags = useIncrementalPushTags.isSelected();
  466. repository.showRemoteBranches = showRemoteBranches.isSelected();
  467. repository.skipSizeCalculation = skipSizeCalculation.isSelected();
  468. repository.skipSummaryMetrics = skipSummaryMetrics.isSelected();
  469. repository.maxActivityCommits = (Integer) maxActivityCommits.getSelectedItem();
  470. repository.isFrozen = isFrozen.isSelected();
  471. repository.allowForks = allowForks.isSelected();
  472. repository.verifyCommitter = verifyCommitter.isSelected();
  473. String ml = mailingListsField.getText();
  474. if (!StringUtils.isEmpty(ml)) {
  475. Set<String> list = new HashSet<String>();
  476. for (String address : ml.split("(,|\\s)")) {
  477. if (StringUtils.isEmpty(address)) {
  478. continue;
  479. }
  480. list.add(address.toLowerCase());
  481. }
  482. repository.mailingLists = new ArrayList<String>(list);
  483. }
  484. repository.accessRestriction = (AccessRestrictionType) accessRestriction
  485. .getSelectedItem();
  486. repository.authorizationControl = allowAuthenticated.isSelected() ?
  487. AuthorizationControl.AUTHENTICATED : AuthorizationControl.NAMED;
  488. repository.federationStrategy = (FederationStrategy) federationStrategy
  489. .getSelectedItem();
  490. if (repository.federationStrategy.exceeds(FederationStrategy.EXCLUDE)) {
  491. repository.federationSets = setsPalette.getSelections();
  492. }
  493. repository.indexedBranches = indexedBranchesPalette.getSelections();
  494. repository.preReceiveScripts = preReceivePalette.getSelections();
  495. repository.postReceiveScripts = postReceivePalette.getSelections();
  496. // Custom Fields
  497. repository.customFields = new LinkedHashMap<String, String>();
  498. if (customTextfields != null) {
  499. for (JTextField field : customTextfields) {
  500. String key = field.getName();
  501. String value = field.getText();
  502. repository.customFields.put(key, value);
  503. }
  504. }
  505. return true;
  506. }
  507. private void error(String message) {
  508. JOptionPane.showMessageDialog(EditRepositoryDialog.this, message,
  509. Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);
  510. }
  511. public void setAccessRestriction(AccessRestrictionType restriction) {
  512. this.accessRestriction.setSelectedItem(restriction);
  513. setupAccessPermissions(restriction);
  514. }
  515. public void setAuthorizationControl(AuthorizationControl authorization) {
  516. boolean authenticated = authorization != null && AuthorizationControl.AUTHENTICATED.equals(authorization);
  517. this.allowAuthenticated.setSelected(authenticated);
  518. this.allowNamed.setSelected(!authenticated);
  519. }
  520. public void setUsers(List<String> owners, List<String> all, List<RegistrantAccessPermission> permissions) {
  521. ownersPalette.setObjects(all, owners);
  522. usersPalette.setObjects(all, permissions);
  523. }
  524. public void setTeams(List<String> all, List<RegistrantAccessPermission> permissions) {
  525. teamsPalette.setObjects(all, permissions);
  526. }
  527. public void setRepositories(List<RepositoryModel> repositories) {
  528. repositoryNames.clear();
  529. for (RepositoryModel repository : repositories) {
  530. // force repo names to lowercase
  531. // this means that repository name checking for rpc creation
  532. // is case-insensitive, regardless of the Gitblit server's
  533. // filesystem
  534. repositoryNames.add(repository.name.toLowerCase());
  535. }
  536. }
  537. public void setFederationSets(List<String> all, List<String> selected) {
  538. setsPalette.setObjects(all, selected);
  539. }
  540. public void setIndexedBranches(List<String> all, List<String> selected) {
  541. indexedBranchesPalette.setObjects(all, selected);
  542. }
  543. public void setPreReceiveScripts(List<String> all, List<String> inherited,
  544. List<String> selected) {
  545. preReceivePalette.setObjects(all, selected);
  546. showInherited(inherited, preReceiveInherited);
  547. }
  548. public void setPostReceiveScripts(List<String> all, List<String> inherited,
  549. List<String> selected) {
  550. postReceivePalette.setObjects(all, selected);
  551. showInherited(inherited, postReceiveInherited);
  552. }
  553. private void showInherited(List<String> list, JLabel label) {
  554. StringBuilder sb = new StringBuilder();
  555. if (list != null && list.size() > 0) {
  556. sb.append("<html><body><b>INHERITED</b><ul style=\"margin-left:5px;list-style-type: none;\">");
  557. for (String script : list) {
  558. sb.append("<li>").append(script).append("</li>");
  559. }
  560. sb.append("</ul></body></html>");
  561. }
  562. label.setText(sb.toString());
  563. }
  564. public RepositoryModel getRepository() {
  565. if (canceled) {
  566. return null;
  567. }
  568. return repository;
  569. }
  570. public List<RegistrantAccessPermission> getUserAccessPermissions() {
  571. return usersPalette.getPermissions();
  572. }
  573. public List<RegistrantAccessPermission> getTeamAccessPermissions() {
  574. return teamsPalette.getPermissions();
  575. }
  576. public void setCustomFields(RepositoryModel repository, Map<String, String> customFields) {
  577. customFieldsPanel.removeAll();
  578. customTextfields = new ArrayList<JTextField>();
  579. final Insets insets = new Insets(5, 5, 5, 5);
  580. JPanel fields = new JPanel(new GridLayout(0, 1, 0, 5)) {
  581. private static final long serialVersionUID = 1L;
  582. @Override
  583. public Insets getInsets() {
  584. return insets;
  585. }
  586. };
  587. for (Map.Entry<String, String> entry : customFields.entrySet()) {
  588. String field = entry.getKey();
  589. String value = "";
  590. if (repository.customFields != null && repository.customFields.containsKey(field)) {
  591. value = repository.customFields.get(field);
  592. }
  593. JTextField textField = new JTextField(value);
  594. textField.setName(field);
  595. textField.setPreferredSize(new Dimension(450, 26));
  596. fields.add(newFieldPanel(entry.getValue(), 250, textField));
  597. customTextfields.add(textField);
  598. }
  599. JScrollPane jsp = new JScrollPane(fields);
  600. jsp.getVerticalScrollBar().setBlockIncrement(100);
  601. jsp.getVerticalScrollBar().setUnitIncrement(100);
  602. jsp.setViewportBorder(null);
  603. customFieldsPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
  604. customFieldsPanel.add(jsp);
  605. }
  606. /**
  607. * ListCellRenderer to display descriptive text about the access
  608. * restriction.
  609. *
  610. */
  611. private class AccessRestrictionRenderer extends DefaultListCellRenderer {
  612. private static final long serialVersionUID = 1L;
  613. @Override
  614. public Component getListCellRendererComponent(JList list, Object value,
  615. int index, boolean isSelected, boolean cellHasFocus) {
  616. super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
  617. if (value instanceof AccessRestrictionType) {
  618. AccessRestrictionType restriction = (AccessRestrictionType) value;
  619. switch (restriction) {
  620. case NONE:
  621. setText(Translation.get("gb.notRestricted"));
  622. break;
  623. case PUSH:
  624. setText(Translation.get("gb.pushRestricted"));
  625. break;
  626. case CLONE:
  627. setText(Translation.get("gb.cloneRestricted"));
  628. break;
  629. case VIEW:
  630. setText(Translation.get("gb.viewRestricted"));
  631. break;
  632. }
  633. } else {
  634. setText(value.toString());
  635. }
  636. return this;
  637. }
  638. }
  639. /**
  640. * ListCellRenderer to display descriptive text about the federation
  641. * strategy.
  642. */
  643. private class FederationStrategyRenderer extends JLabel implements
  644. ListCellRenderer {
  645. private static final long serialVersionUID = 1L;
  646. @Override
  647. public Component getListCellRendererComponent(JList list, Object value,
  648. int index, boolean isSelected, boolean cellHasFocus) {
  649. if (value instanceof FederationStrategy) {
  650. FederationStrategy strategy = (FederationStrategy) value;
  651. switch (strategy) {
  652. case EXCLUDE:
  653. setText(Translation.get("gb.excludeFromFederation"));
  654. break;
  655. case FEDERATE_THIS:
  656. setText(Translation.get("gb.federateThis"));
  657. break;
  658. case FEDERATE_ORIGIN:
  659. setText(Translation.get("gb.federateOrigin"));
  660. break;
  661. }
  662. } else {
  663. setText(value.toString());
  664. }
  665. return this;
  666. }
  667. }
  668. }