Advertisement
Guest User

Untitled

a guest
Dec 8th, 2011
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.83 KB | None | 0 0
  1.  
  2. namespace Views
  3. {
  4.     /// <summary>
  5.     /// Okno tworzenia nowego szablonu Excel
  6.     /// </summary>
  7.     public partial class NewExcelTemplatePopUpWindow : ChildWindow
  8.     {
  9.         /// <summary>
  10.         /// WebService obsługi excel'a
  11.         /// </summary>
  12.         private ExcelReaderWSClient excelReaderWS;
  13.  
  14.         /// <summary>
  15.         /// Dialog otwarcia pliku
  16.         /// </summary>
  17.         private OpenFileDialog openFile;
  18.  
  19.         /// <summary>
  20.         /// Binarny stream pliku Excel
  21.         /// </summary>
  22.         private byte[] stream;
  23.  
  24.         /// <summary>
  25.         /// Flaga określająca czy plik został
  26.         /// przesłany poprawnie
  27.         /// </summary>
  28.         private bool isFileUploaded;
  29.  
  30.         /// <summary>
  31.         /// Datasource
  32.         /// </summary>
  33.         private DomainDataSource domainDataSource;
  34.  
  35.         /// <summary>
  36.         /// Podstawowy konstruktor
  37.         /// </summary>
  38.         public NewExcelTemplatePopUpWindow()
  39.         {
  40.             InitializeComponent();
  41.             excelReaderWS = new ExcelReaderWSClient();
  42.             this.fUploadProgressBar.Visibility = System.Windows.Visibility.Collapsed;
  43.             this.filePath.IsReadOnly = true;
  44.             this.ChangeUserBoxesState(false);
  45.  
  46.             // Stwórz databinding sumujący liczbę kolumn:
  47.             // Nazwa property danego obiektu:
  48.             Binding myBinding = new Binding("Count");
  49.             // Obiekt zawierajacy properte:
  50.             myBinding.Source = this.columnNamesList.Items;
  51.             // Przypisz binding
  52.             this.columnCount.SetBinding(Label.ContentProperty, myBinding);
  53.  
  54.             this.browseButton.Click += new RoutedEventHandler(BrowseButton_Click);
  55.             this.uploadButton.Click += new RoutedEventHandler(UploadButton_Click);
  56.             this.okButton.Click += new RoutedEventHandler(OKButton_Click);
  57.             this.cancelButton.Click += new RoutedEventHandler(CancelButton_Click);
  58.             this.Unloaded += new RoutedEventHandler(NewExcelTemplatePopUpWindow_Unloaded);
  59.             this.excelReaderWS.GetColumnNamesCompleted += new EventHandler<GetColumnNamesCompletedEventArgs>(ExcelReaderWS_GetColumnNamesCompleted);
  60.  
  61.         }
  62.  
  63.         /// <summary>
  64.         ///
  65.         /// </summary>
  66.         /// <param name="sender"></param>
  67.         /// <param name="e"></param>
  68.         private void NewExcelTemplatePopUpWindow_Unloaded(object sender, RoutedEventArgs e)
  69.         {
  70.             this.excelReaderWS.CloseAsync();
  71.             this.openFile = null;
  72.  
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Konstruktor tworzący nowe okno do wczytania pliku excel'a
  77.         /// </summary>
  78.         /// <param name="excelDomainDataSource">Kontektst usługi nadrzędnej</param>
  79.         public NewExcelTemplatePopUpWindow(DomainDataSource excelDomainDataSource)
  80.             : this()
  81.         {
  82.             this.domainDataSource = excelDomainDataSource;
  83.         }
  84.  
  85.         /// <summary>
  86.         /// Aktualizuje informację o postępie w procesie aplikacji.
  87.         /// </summary>
  88.         /// <param name="information">Informacja</param>
  89.         private void UpdateStatusInformation(string information)
  90.         {
  91.             this.progresBarInformation.Content = information;
  92.         }
  93.  
  94.         /// <summary>
  95.         /// Zmienia stan formatek danych
  96.         /// wprowadzanych przez użytkownika.
  97.         /// Nieaktywne formatki są resetowane.
  98.         /// </summary>
  99.         /// <param name="areEnabled"><c>true</c> dla aktywacji formatek</param>
  100.         private void ChangeUserBoxesState(bool areEnabled)
  101.         {
  102.             this.templateNameBox.IsReadOnly = !areEnabled;
  103.             if (!areEnabled)
  104.                 this.templateNameBox.Text = "";
  105.  
  106.             this.templateDescBox.IsReadOnly = !areEnabled;
  107.             if (!areEnabled)
  108.                 this.templateDescBox.Text = "";
  109.         }
  110.  
  111.         /// <summary>
  112.         /// Akcja po kliknięciu w przycisk przeglądaj.
  113.         /// </summary>
  114.         /// <param name="sender"></param>
  115.         /// <param name="e"></param>
  116.         private void BrowseButton_Click(object sender, RoutedEventArgs e)
  117.         {
  118.             // Stwórz dialog:
  119.             this.openFile = new OpenFileDialog();
  120.             openFile.Multiselect = false;
  121.             openFile.Filter = "Microsoft Excel 97-2003 file | *.xls";
  122.  
  123.             // Zaktualizuj status i otwórz plik:
  124.             if (this.BoolConversion(openFile.ShowDialog()))
  125.             {
  126.                 // Przygotuj kontrolki:
  127.                 this.ChangeUserBoxesState(false);
  128.                 this.columnNamesList.Items.Clear();
  129.                 this.UpdateStatusInformation(LANOS.Assets.Resources.ApplicationStrings.OpeningFile);
  130.                 this.fUploadProgressBar.Visibility = System.Windows.Visibility.Visible;
  131.                 this.fUploadProgressBar.IsIndeterminate = true;
  132.                 this.isFileUploaded = false;
  133.                 this.filePath.Text = openFile.File.Name;
  134.  
  135.                 // Przeczytaj binarnie plik:
  136.                 this.stream = null;
  137.                 FileStream fs = openFile.File.OpenRead();
  138.                 stream = this.ReadFully(fs, 0);
  139.                 fs.Close();
  140.  
  141.                 // Ukryj progress bar:
  142.                 this.UpdateStatusInformation("");
  143.                 this.fUploadProgressBar.Visibility = System.Windows.Visibility.Collapsed;
  144.             }
  145.             else
  146.             {
  147.                 // Ukryj progress bar i pokaż informacje o niepowodzeniu:
  148.                 this.fUploadProgressBar.Visibility = System.Windows.Visibility.Collapsed;
  149.                 this.UpdateStatusInformation(LANOS.Assets.Resources.ApplicationStrings.FileOpenFailed);
  150.             }
  151.         }
  152.  
  153.         /// <summary>
  154.         /// Akcja po kliknięciu w przycisk upload
  155.         /// </summary>
  156.         /// <param name="sender"></param>
  157.         /// <param name="e"></param>
  158.         private void UploadButton_Click(object sender, RoutedEventArgs e)
  159.         {
  160.             // Prześlij stream do Web Service'u do analizy:
  161.             if (this.stream != null)
  162.             {
  163.                 this.columnNamesList.Items.Clear();
  164.                 this.UpdateStatusInformation(LANOS.Assets.Resources.ApplicationStrings.GettingColumnNames);
  165.                 this.fUploadProgressBar.Visibility = System.Windows.Visibility.Visible;
  166.                 this.excelReaderWS.GetColumnNamesAsync(this.stream, openFile.File.Name);
  167.             }
  168.         }
  169.  
  170.         /// <summary>
  171.         /// Akcja wykonywana po otrzymania odpowiedzi Web Service
  172.         /// </summary>
  173.         /// <param name="sender"></param>
  174.         /// <param name="e"></param>
  175.         private void ExcelReaderWS_GetColumnNamesCompleted(object sender, GetColumnNamesCompletedEventArgs e)
  176.         {
  177.             // Ukryj pasek progresu:
  178.             this.fUploadProgressBar.Visibility = System.Windows.Visibility.Collapsed;
  179.             if (e.Error == null)
  180.             {
  181.                 // Aktywuj kontrolki użytkownika:
  182.                 this.isFileUploaded = true;
  183.                 this.UpdateStatusInformation("");
  184.                 this.ChangeUserBoxesState(true);
  185.                 this.templateNameBox.Text = this.filePath.Text.Replace(".xls", "");
  186.                 ObservableCollection<string> result = e.Result;
  187.  
  188.                 // Dodaj nazwy kolumn:
  189.                 foreach (string column in result)
  190.                 {
  191.                     this.columnNamesList.Items.Add(column);
  192.                 }
  193.             }
  194.             else
  195.             {
  196.                 this.UpdateStatusInformation(LANOS.Assets.Resources.ApplicationStrings.GettingColumnsFailed);
  197.             }
  198.         }
  199.  
  200.         /// <summary>
  201.         /// Akcja po kliknięciu w przycisk OK
  202.         /// </summary>
  203.         /// <param name="sender"></param>
  204.         /// <param name="e"></param>
  205.         private void OKButton_Click(object sender, RoutedEventArgs e)
  206.         {
  207.             // Sprawdź czy plik załadowany jest poprawnie
  208.             if (this.isFileUploaded)
  209.             {
  210.                 // Waliduj:
  211.                 if (this.ValidateTemplateInformation())
  212.                 {
  213.                     List<string> result = new List<string>();
  214.                     foreach (string s in this.columnNamesList.Items)
  215.                     {
  216.                         result.Add(s);
  217.                     }
  218.                     string xmlColumnList = this.SerializeToXMLString(typeof(List<string>), result);
  219.  
  220.                     // Zapisz nowy szablon i zakończ:
  221.                     ExcelTemplate template = new ExcelTemplate();
  222.                     template.CreationDate = DateTime.Now;
  223.                     template.TemplateName = this.templateNameBox.Text;
  224.                     template.Description = this.templateDescBox.Text;
  225.                     template.XmlColumnNames = xmlColumnList;
  226.  
  227.                     this.domainDataSource.DataView.Add(template);
  228.                     this.domainDataSource.SubmitChanges();
  229.                     this.DialogResult = true;
  230.                 }
  231.             }
  232.         }
  233.  
  234.         /// <summary>
  235.         /// Waliduje informacje o
  236.         /// szablonie wprowadzane przez użytkownika
  237.         /// </summary>
  238.         /// <returns><c>true</c> jeśli walidacja przebiegła poprawnie</returns>
  239.         private bool ValidateTemplateInformation()
  240.         {
  241.  
  242.         }
  243.  
  244.         /// <summary>
  245.         /// Wyszukuje szablon o wskazanej nazwie
  246.         /// w zbiorze obecnych szablonów
  247.         /// </summary>
  248.         /// <param name="name">Nazwa szablonu</param>
  249.         /// <returns><c>true</c> jeśli nazwa szablonu jest unikatowa</returns>
  250.         public bool IsTemplateNameUnique(string name)
  251.         {
  252.             foreach (ExcelTemplate template in this.domainDataSource.DataView)
  253.             {
  254.                 if (name.ToLower().Equals(template.TemplateName.ToLower()))
  255.                 {
  256.                     return false;
  257.                 }
  258.             }
  259.  
  260.             return true;
  261.         }
  262.  
  263.         /// <summary>
  264.         /// Zamyka i anuluje akcje tworzenia
  265.         /// szablonu
  266.         /// </summary>
  267.         /// <param name="sender"></param>
  268.         /// <param name="e"></param>
  269.         private void CancelButton_Click(object sender, RoutedEventArgs e)
  270.         {
  271.             this.DialogResult = false;
  272.         }
  273.  
  274.         /// <summary>
  275.         /// Akcja po zamknięciu okna
  276.         /// </summary>
  277.         /// <param name="sender"></param>
  278.         /// <param name="e"></param>
  279.         private void ChildWindow_Closed(object sender, EventArgs e)
  280.         {
  281.             this.DialogResult = false;
  282.         }
  283.  
  284.         /// <summary>
  285.         /// Akcja po kliknięciu przycisku anuluj
  286.         /// </summary>
  287.         /// <param name="sender"></param>
  288.         /// <param name="e"></param>
  289.         private void cancelButton_Click_1(object sender, RoutedEventArgs e)
  290.         {
  291.             this.DialogResult = false;
  292.         }
  293.     }
  294. }
  295.  
  296.  
  297.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement